mongo 附近将 maxDistance 设置为 collection 中的值

mongo near set maxDistance as value in collection

我对标题感到抱歉。我很难描述。我有这个 collection

  {
      "_id" : ObjectId("55cb9c666c522cafdb053a68"),
      location: {
     type: "Point",
     coordinates: [-73.856077, 40.848447]
  },
      "maxDistancevalue" : 100000
  }

现在我要查找的当前位置是在: 100000 定义的 collection by "maxDistancevalue"

代码将是这样的。但是如何设置最大距离值呢?

  db.places.find(
     {
       location:
         { $near :
            {
              $geometry: { type: "Point",  coordinates: [ -73.9667, 40.78 ] },
              $minDistance: **??????, -->maxDistancevalue** 
              $maxDistance: 0
            }
         }
     }
  ) 

您可以使用聚合框架的 $geoNear 管道阶段来引用其他现有字段。它需要在您的 collection 上创建 2dsphere 索引,因此从:

开始
db.places.createIndex({location:"2dsphere"});

然后您可以 运行 您的 aggregate() 查询:

db.places.aggregate([
    {
        $geoNear: {
            near: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
            distanceField: "dist.calculated",
            maxDistance: "$maxDistancevalue"
        }
    }
])