在嵌入文档中存储2dsphere位置并查询位置字段

时间:2015-04-14 07:42:30

标签: node.js mongodb mongoose 2dsphere

我正在尝试这个模型架构,我不确定我是否能够以这种方式查询位置字段。

   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);

我试过这个

  garage.find({ location: { '$near': {  type : "Point" ,
                            coordinates : [ geometry[0] , geometry[1] ] } 
                  } } ).populate("user_id").populate("locations").exec()...

我没有错误但也没有结果。

我是否需要管理主车库架构中的位置(未引用)? 它可以这样工作吗?

谢谢你的想法!

0 个答案:

没有答案
相关问题