【发布时间】:2018-11-27 20:51:19
【问题描述】:
我的数据库中有 2 个集合。一个叫用户
{
_id: storeUserId,
name: ...,
etc
}
另一个叫Following
{
userId: ...,
followingUserId: ...
}
userId是当前用户id,followingUserId是当前用户想要关注的id。
例如,在用户集合中我有:
{
_id: userIdOne,
etc
},
{
_id: userIdTwo,
etc
}
在以下收藏中我有:
{
userId: userIdThousand,
followingUserId: userIdTwo
}
当我运行查找查询时
db.bios.find();
我明白了
{
"_id": userIdTwo,
"username": "random27005688"
},
{
"_id": userIdThree
"username": "random232111"
},
{
"_id": userIdOne
"username": "random2702"
}
]
结果是我想要的,但我想为每个结果项添加一个“isFollowed”字段以检查以下状态。我有一个用户 ID,可以说:'userIdThousand',我想用它来检查基于我的以下集合的每个结果项。例如,
check if userIdThousand is following userIdOne
check if userIdThousand is following userIdTwo, etc.
以下是我的预期结果。谢谢!
[
{
_id: userIdTwo,
"username": "random27005688",
"isFollowed": true
},
{
"_id": userIdThree
"username": "random232111",
"isFollowed": false
},
{
"_id": userIdOne
"username": "random2702",
"isFollowed": false
},
]
【问题讨论】:
-
你能从查询中发布你想要的输出吗
-
我想我的问题已经有了。基本上我只需要一个“isFolwed”字段来检查关注状态
标签: node.js mongodb mongoose mongodb-query