I am trying this model schema and i am not sure if i will be able to query the locations field this way.
var garageSchema = new Schema({
user_id :{ type: Schema.Types.ObjectId, ref: "User" },
locations :[{ type: Schema.Types.ObjectId, ref: "Location" }],
});
Schema.index({"locations.location": '2dsphere'});
var garage = mongoose.model( 'Garage', garageSchema);
var locationSchema = new Schema({
name:{ type: String, trim: true},
location:{
'type': { type: String, enum: "Point", default: "Point"},
coordinates: { type: [Number], default: [0,0]}
},
});
//this part is in location model in a different file
locationSchema.index({location: '2dsphere'});
var location = mongoose.model( 'Location', locationSchema);
I tried this
garage.find({ location: { '$near': { type : "Point" ,
coordinates : [ geometry[0] , geometry[1] ] }
} } ).populate("user_id").populate("locations").exec()...
i get no error but no results either.
do i need to manage the locations in the main garage schema(not referenced )? can it work this way ?
thank you for thinking of it!