Skip to content

Instantly share code, notes, and snippets.

@DouglasHennrich
Last active February 1, 2018 13:34
Show Gist options
  • Save DouglasHennrich/735c4af4de2d38656a95c97318742b78 to your computer and use it in GitHub Desktop.
Save DouglasHennrich/735c4af4de2d38656a95c97318742b78 to your computer and use it in GitHub Desktop.
CityHall Schema.js
{
"_id": ObjectId("5a73112180820986b109e6b8"),
"departments": [
{
"drivers": [
{
"loc": {
"coordinates": [
-12.45,
45.67
],
"type": "Point"
},
"_id": ObjectId("5a73112180820986b109e6bb"),
"socketId": "String",
"guid": "123"
},
{
"loc": {
"coordinates": [
-12.45,
45.67
],
"type": "Point"
},
"_id": ObjectId("5a73112180820986b109e6ba"),
"socketId": "String",
"guid": "abc"
}
],
"users": [ ],
"_id": ObjectId("5a73112180820986b109e6b9"),
"department": 5
}
],
"cityHall": 2,
"__v": 0
}
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Drivers Schema
const driversSchema = new Schema({
socketId: String
, loc: {
type: { type: String }
, coordinates: [Number]
}
, car: Object
, driving: Object
, guid: { type: String, index: true, unique: true }
});
driversSchema.index({ loc: '2dsphere' });
driversSchema.index({ guid: 1 });
// Users Schema
const usersSchema = new Schema({
socketId: String
, car: Object
, server: Object
, guid: { type: String, index: true, unique: true }
});
usersSchema.index({ guid: 1 });
// Departments Schema
const departmentsSchema = new Schema({
department: { type: Number, unique: true, index: true }
, drivers: [driversSchema]
, users: [usersSchema]
});
departmentsSchema.index({ department: 1 });
// City Hall Schema
const cityHallsSchema = {
cityHall: { type: Number, unique: true }
, departments: [departmentsSchema]
};
// Model
const Model = mongoose.model('cityHalls', new Schema(cityHallsSchema));
// Query
const query = [
{
$match: { cityHall: 2 }
}
, {
$unwind: '$departments'
}
, {
$match: { 'departments.department': 5 }
}
, {
$unwind: '$departments.drivers'
}
, {
$match: { 'drivers.guid': '123' }
}
];
// Mod
// const mod = {
// upsert: true
// };
// toInsert
// let toInsert = {
// cityHall: 2
// , departments: [
// {
// department: 5
// , drivers: [
// {
// socketId: 'String'
// , loc: {
// type: 'Point'
// , coordinates: [
// -12.45
// , 45.67
// ]
// }
// , car: {
//
// }
// , driving: {
//
// }
// , guid: '123'
// }
//
// , {
// socketId: 'String'
// , loc: {
// type: 'Point'
// , coordinates: [
// -12.45
// , 45.67
// ]
// }
// , car: {
//
// }
// , driving: {
//
// }
// , guid: 'abc'
// }
// ]
// , users: [ ]
// }
// ]
// };
//
// Model.create(toInsert, (err, data) => {
// if (err) return console.log('err:', err);
//
// console.warn('============');
// console.warn('data:', JSON.stringify(data));
// console.warn('============');
// });
Model.aggregate(query, (err, data) => {
if (err) return console.log('err:', err);
console.warn('============');
console.warn('data:', JSON.stringify(data));
console.warn('============');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment