对不起,标题。我很难形容。我有这个收藏
{
"_id" : ObjectId("55cb9c666c522cafdb053a68"),
location: {
type: "Point",
coordinates: [-73.856077, 40.848447]
},
"maxDistancevalue" : 100000
}
现在我要查找的当前位置在:100000定义为“ maxDistancevalue”的集合
代码将像这样。但是如何设置maxDistancevalue?
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: **??????, -->maxDistancevalue**
$maxDistance: 0
}
}
}
)
答案 0 :(得分:1)
您可以使用Aggregation Framework的$geoNear管道阶段来引用其他现有字段。它需要在您的集合上创建2dsphere索引,因此从以下开始:
db.places.createIndex({location:"2dsphere"});
然后您可以运行aggregate()
查询:
db.places.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
distanceField: "dist.calculated",
maxDistance: "$maxDistancevalue"
}
}
])