【发布时间】:2018-03-02 15:58:12
【问题描述】:
我有两个集合 Members 和 MobileUserLocations - 每个用户的位置都保存(可以是多个)作为外部字段的 userId。
成员:
{
_id: ObjectId("591553ffa4233a181506880c"),
userName: "Test user"
}
MobileUserLocations:
{ _id: ObjectId("59156070a4233a1815068b6b"),
userId: ObjectId("591553ffa4233a181506880c"),
location: {type: "Point", coordinates: [76.9121, 10.2232]]},
updatedOn: 2017-05-12T07:12:48.626Z,
status: 1
},
{ _id: ObjectId("59156070a4233a1815068b6b"),
userId: ObjectId("591553ffa4233a181506880c"),
location: {type: "Point", coordinates: [76.8121, 10.1232]]},
updatedOn: 2017-05-12T07:12:48.626Z,
status: 1
}
我想获取半径范围内的成员 - 例如参考特定地理点 5 公里 - 例如:[10.0132295, 76.3630502](lat,lng 格式)。
我试过了:
collection.aggregate([
{$match: {_id: { $ne: options.accessToken.userId }},
{ "$lookup": {
"localField": "_id",
"from": "MobileUserLocations",
"foreignField": "userId",
"as": "userLocInfo"
}
},
{
$project: {
_id: 1,
userLocInfo: {
"$filter": {
"input": "$userLocInfo",
"as": "userLoc",
"cond": {
"$eq": [ "$$userLoc.status", -1],
"$$userLoc.location": {"$geoWithin": {"$centerSphere": [[76.3630502, 10.0132295], 5 / 3963.2]}}
}
}
}
}
},
{$unwind: "$userLocInfo"}
]
但没有得到。如果我从过滤条件中删除 $geowithin,它会得到,否则不会得到。但如果我是单独查询集合,我会得到结果。
谁能知道这个问题?
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework