使用$ geoNear聚合时,Mongoose查询返回错误的结果

时间:2017-07-21 09:17:53

标签: node.js mongodb mongoose

我有以下mongoose查询:

User.aggregate([
      {
        $geoNear: {
          near: {type: 'Point', coordinates: [ 1 , 1 ]},
          distanceField: 'dis',
          maxDistance: '$comRange' * 1000,
          spherical: true
        }
      },
      {
        $match: {isVolunteer: true, 'devices.enabled': true, }
      },
      {
        $group: { _id: '$devices.deviceID'.replace(/"/g, '')}
      }
    ],
    (err, locations) => {
      console.log('locations',JSON.stringify(locations, null, 4))
    }
  );

我在文档中有另一个包含用户坐标的字段:

volAddressCoords    : {type: Object, properties: {type: {type: String, enum: 'Point', default: 'Point'}, coordinates: {type: [Number], default: [0, 0]}, index: '2dsphere'}},
comRange            : {type: Number, min: 1, max: 20},
devices             : {type: Array}
在其他路径中接收POST请求时会创建

设备:

let device = {
      enabled: true,
      deviceID : req.body.deviceId
    };

无论我穿上什么坐标值(靠近或远离comRange)

near: {type: 'Point', coordinates: [ 1 , 1 ]}

它总会返回:

locations [
  {
    "_id": [
        "\"e_Fhn7sWzXE:APA91bGb5xpw4o\"",
        "\"eI2K-6iUvVw:APA91bGrdwh29H\""
           ]
  }
]

我在这里缺少什么?当坐标远时,应该有一个空白数组,当坐标在comRange内时,它应该填充数组。

除了修复此查询之外,还有更好的方法来改进此查询吗?

0 个答案:

没有答案
相关问题