2017-04-10 103 views
0

我不明白为什么我的查询不适用于MongoDb $ geoNear?

我检查这个问题没有成功:mongodb geoNear command with filter

我的环境:

  • 的NodeJS:V6.6.0
  • 猫鼬:4.8.6

这个作品

db.collection.geoNear(
    coords, 
    { 
    query : { status: "1" } }, // I have the good filtered results 
    ... 

我已经测试

... 
query : { _id: { $in: itemIds } }, //no error and empty result 
query : { "_id": "5639ce8c01f7d527089d2a74" }, //no error and empty result 
query : { "_id" : 'ObjectId("5649e9f35f0b360300cad022")' }, //no error and empty result 
query : { "_id" : ObjectId("5649e9f35f0b360300cad022") }, //error ObjectId not defined 
... 

我想这

db.collection.geoNear(
    coords, 
    { 
    query : { _id: { $nin: poiIds } }, // no error but I have not the good results because I obtain all results geolocalized 
    ... 

谢谢;-)

回答

0

针对此问题:

查询:{ “_id”:物件( “5649e9f35f0b360300cad022”)} //错误OBJECTID没有定义

尝试:

const ObjectID = require('mongodb').ObjectID 
0

在我的情况,我没有这样说:

将索引添加到模型中的坐标,然后请求查询。

modelSchema.index({ coords: '2dsphere' }); 

query = { 
    _id: { 
    $nin: poiIds 
    }, 
    coords: { 
    $near: { 
     $geometry: { type: "Point", coordinates: position } 
    } 
    } 
} 

其中position是一个数组[Lat, Lon]

希望这会有所帮助。 ;)