gpt4 book ai didi

node.js - 获取同时具有关注者和关注者的共同列表

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:45 24 4
gpt4 key购买 nike

我有一些架构名称 follower哪里user字段包含正在跟踪的值和 follower是上面关注的人 user 。我想要正在关注并且同时也被同一个人关注的用户列表。下面是集合 follower 。我需要像 "user" : ObjectId("5e09e4ab35f4a75e8d8536a9"), "follower" : ObjectId("5e15a8763a8ec3019f6f9652"), 这样的条目和"user" : ObjectId("5e15a8763a8ec3019f6f9652"), "follower" : ObjectId("5e09e4ab35f4a75e8d8536a9"),因为他们都互相关注。

/* 1 */
{
"_id" : ObjectId("5e15c0c03a8ec3019f6f96c4"),
"createdAt" : "1578395182056",
"user" : ObjectId("5e09e4ab35f4a75e8d8536a9"),
"follower" : ObjectId("5e15a8763a8ec3019f6f9652"),
"__v" : 0
}

/* 2 */
{
"_id" : ObjectId("5e1700852ad51f163b434d23"),
"createdAt" : "1578495739228",
"user" : ObjectId("5e15a8763a8ec3019f6f9652"),
"follower" : ObjectId("5e16f8892ad51f163b434ca6"),
"__v" : 0
}

/* 3 */
{
"_id" : ObjectId("5e1c82222ad51f163b434db1"),
"createdAt" : "1578495739228",
"user" : ObjectId("5e09e4ab35f4a75e8d8536a9"),
"follower" : ObjectId("5e1c780c2ad51f163b434dae"),
"__v" : 0
}

/* 4 */
{
"_id" : ObjectId("5e21da4004925e327f6971f6"),
"createdAt" : "1579257833519",
"user" : ObjectId("5e09e4ab35f4a75e8d8536a9"),
"follower" : ObjectId("5e21967f04925e327f6971a0"),
"__v" : 0
}

/* 5 */
{
"_id" : ObjectId("5e22b8ddc30a562d3b3238e8"),
"createdAt" : "1579328869333",
"user" : ObjectId("5e21967f04925e327f6971a0"),
"follower" : ObjectId("5e09e4ab35f4a75e8d8536a9"),
"__v" : 0
}

/* 6 */
{
"_id" : ObjectId("5e22babfc30a562d3b3238ec"),
"createdAt" : "1579328869333",
"user" : ObjectId("5e09fbd035f4a75e8d8536c1"),
"follower" : ObjectId("5e09e4ab35f4a75e8d8536a9"),
"__v" : 0
}

/* 7 */
{
"_id" : ObjectId("5e26b580630be3588b5ed152"),
"createdAt" : "1579594730310",
"user" : ObjectId("5e09e76035f4a75e8d8536ae"),
"follower" : ObjectId("5e09e4ab35f4a75e8d8536a9"),
"__v" : 0
}

/* 8 */
{
"_id" : ObjectId("5e27101527bb4366a8ee5d54"),
"createdAt" : "1579614413346",
"user" : ObjectId("5e09e7d335f4a75e8d8536af"),
"follower" : ObjectId("5e09e4ab35f4a75e8d8536a9"),
"__v" : 0
}

最佳答案

您可以使用以下管道:

db.collection.aggregate(
[
{
"$lookup": {
"from": "followers", //this is the current collection, I didn't know what's the name
"let": {
"currUser": "$user",
"currFollower": "$follower"
},
"pipeline": [
{
"$match": {
"$expr": {
"$and": [
{
"$eq": [
"$user",
"$$currFollower"
]
},
{
"$eq": [
"$follower",
"$$currUser"
]
}
]
}
}
}
],
"as": "matched"
}
},
{
"$match": {
"matched.0": {
"$exists": true
}
}
},
{
"$group": {
"_id": null,
"usersThatFollowEachOther": {"$addToSet": "$follower"}
}
}
]
);

关于node.js - 获取同时具有关注者和关注者的共同列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59859658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com