0

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!

Crimeira
  • 149
  • 10
  • No. It looks like you want to do a join. You need to store the data, not a reference to the data, in the document. – wdberkeley Apr 14 '15 at 15:38
  • if i was storing the 2dsphere locations in the array and not referencing them do you think i would be able to query them ? – Crimeira Apr 16 '15 at 12:34

0 Answers0