【问题标题】:NodeJs : $nin array query in $geoNear in MongoDb doesn't workNodeJs:MongoDb 中 $geoNear 中的 $nin 数组查询不起作用
【发布时间】:2017-09-05 05:11:34
【问题描述】:

我不明白为什么我的查询不适用于 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
  ...

谢谢 ;-)

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query


    【解决方案1】:

    对于这个问题:

    query : { "_id" : ObjectId("5649e9f35f0b360300cad022") }, //error ObjectId not defined

    试试:

    const ObjectID = require('mongodb').ObjectID
    

    【讨论】:

      【解决方案2】:

      就我而言,我是这样做的:

      为模型中的坐标添加索引,然后通过查询请求。

      modelSchema.index({ coords: '2dsphere' });
      
      query = {
        _id: {
          $nin: poiIds
        },
        coords: {
          $near: {
            $geometry: { type: "Point", coordinates: position }
          }
        }
      }
      

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

      希望这会有所帮助。 ;)

      【讨论】:

        猜你喜欢
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 2016-07-24
        • 2017-07-18
        • 2016-06-09
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多