我有两个系列:users
-所有用户信息'partnership'-用户可以合作/跟随对方。
**目标:**按最后合作日期对所有合作关系进行排序。
**用户何时成为合作伙伴:**假设user_1喜欢user_2,并且仅当user_2也喜欢user_1时,他们才成为合作伙伴(相互喜欢)
**什么是合作日期:**第二个喜欢(相互喜欢)发生的日期/时间。
我创建了一个mongoPlayground,但请忽略我的查询-Mongo Playground
这是我的示例数据
db={
partnership: [
{
_id: "xyz_rrr",
updated: "2022-10-23T12:35:24.772+00:00",
users: [
"xyz",
"rrr"
]
},
{
_id: "rrr_eee",
updated: "2022-12-23T12:35:24.772+00:00",
users: [
"rrr",
"eee"
]
},
{
_id: "eee_rrr",
updated: "2023-01-21T12:35:24.772+00:00",
users: [
"eee",
"rrr"
]
},
{
_id: "mmm_rrr",
updated: "2023-02-19T12:35:24.772+00:00",
users: [
"mmm",
"rrr"
]
},
{
_id: "rrr_mmm",
updated: "2023-02-21T12:35:24.772+00:00",
users: [
"rrr",
"mmm"
]
},
],
users: [
{
_id: "abc",
name: "abc",
group: 1,
location: {
type: "Point",
coordinates: [
53.23,
67.12
]
},
calculatedDist: 112
},
{
_id: "xyz",
name: "xyyy",
group: 1,
location: {
type: "Point",
coordinates: [
54.23,
67.12
]
},
calculatedDist: 13
},
{
_id: "123",
name: "yyy",
group: 1,
location: {
type: "Point",
coordinates: [
54.23,
67.12
]
},
calculatedDist: 13
},
{
_id: "rrr",
name: "rrrrrrr",
group: 1,
location: {
type: "Point",
coordinates: [
51.23,
64.12
]
},
calculatedDist: 14
},
{
_id: "mmm",
name: "mmmm",
group: 1,
location: {
type: "Point",
coordinates: [
51.23,
64.12
]
},
calculatedDist: 14
},
{
_id: "eee",
name: "eeeee",
group: 1,
location: {
type: "Point",
coordinates: [
55.23,
62.12
]
},
calculatedDist: 143
}
],
}
预期成果
{
partneredUsers:
{ firstUser :
{
_id: "mmm",
name: "mmmm",
},
},
secondUser :
{
_id: "rrr",
name: "rrrrrrr",
},
},
partneredDate: "2023-02-21T12:35:24.772+00:00",
},
{
partneredUsers:
{ firstUser :
{
_id: "rrr",
name: "rrrrrrr",
},
},
secondUser :
{
_id: "eee",
name: "eeeee",
}
},
partneredDate: "2023-01-23T12:35:24.772+00:00",
}
}
1条答案
按热度按时间wmomyfyw1#
从
partnership
集合开始,通过$sortArray
和$concat
创建一个分区键来标识彼此喜欢的关系(他们将共享同一个键),使用$setWindowFields
中的分区键来计算计数和排名。最后
$lookup
从users
得到用户的详细信息,$sort
通过partneredDate
得到。Mongo Playground