我有两个单独工作的功能,但想将它们组合起来。
- 功能 1 - 按地理邻近距离对用户进行排序。
- 功能 2 - 用户不应该已经被喜欢
当前用户(查找
partnership
收藏)
如何更新此查询以从用户的集合开始,以便我可以执行 geoNear?
下面 mongoplayground 中的输出是正确的,只是生成的用户未按以下顺序排序calculatedDist
这是由 geoNear 计算的字段。
$geoNear: {
near: { type: "Point", coordinates: [x,y },
distanceField: "calculatedDist",
spherical: true
}
geoNear 需要仅在用户集合中可用的位置,因此我认为需要修改以下查询以在用户集合中启动。https://mongoplayground.net/p/7H_NxciKezB https://mongoplayground.net/p/7H_NxciKezB
db={
users: [
{
_id: "abc",
name: "abc",
group: 1,
location: {
type: "Point",
coordinates: [
54.23,
67.12
]
},
calculatedDist: 13
},
{
_id: "xyz",
name: "xyyy",
group: 1,
location: {
type: "Point",
coordinates: [
54.23,
67.12
]
},
calculatedDist: 11
},
{
_id: "123",
name: "yyy",
group: 1,
location: {
type: "Point",
coordinates: [
54.23,
67.12
]
},
calculatedDist: 2
},
{
_id: "rrr",
name: "tttt",
group: 1,
location: {
type: "Point",
coordinates: [
54.23,
67.12
]
},
calculatedDist: 11
},
{
_id: "eee",
name: "uuu",
group: 1,
location: {
type: "Point",
coordinates: [
54.23,
67.12
]
},
calculatedDist: 7
},
],
partnership: [
{
_id: "abc_123",
fromUser: "abc",
toUser: "123"
},
{
_id: "eee_rrr",
fromUser: "eee",
toUser: "rrr"
},
{
_id: "rrr_abc",
fromUser: "rrr",
toUser: "abc"
},
{
_id: "abc_rrr",
fromUser: "abc",
toUser: "rrr"
},
{
_id: "xyz_rrr",
fromUser: "xyz",
toUser: "rrr"
},
{
_id: "rrr_eee",
fromUser: "rrr",
toUser: "eee"
},
]
}
据我所知,geoNear 必须是首先要做的事情,所以我的查询应该从users
收藏。这破坏了我的合作伙伴关系检查,因为要使其发挥作用,我从partnership
收藏。
在上面的操场上,用户eee
由于 geoNear,计算距离较小,但它在用户之后显示abc
.