小编MLW*_*DEV的帖子

MongoDB、Mongoose 查找所有 _id 等于数组中对象键的位置

在查询中遇到困难,我有一组用户:

{
    "_id": "5c87bda25fdaasdf00171001e1",
    "name": "UserTwo Name",
    "email": "email2@example.com"
},{
    "_id": "5c87bda25fda8b00171001e1",
    "name": "User Name",
    "email": "email@example.com"
}
Run Code Online (Sandbox Code Playgroud)

我还有一个包含 _userId 键的对象数组。我需要获取用户 _id 等于数组中 _userId 值的所有用户。

[
    {_userId: "5c87bda25fda8b00171001e1"},
    {_userId: "5c87bda25fdaasdf00171001e1"},
]
Run Code Online (Sandbox Code Playgroud)

到目前为止我所拥有的是:

User.find(
        {
            _id: { $in: req.body.users }
        }
    )
Run Code Online (Sandbox Code Playgroud)

如果 req.body.users 只是一个 ids 数组,而不是对象数组,那应该没问题。

任何帮助将不胜感激。谢谢,

mongoose mongodb node.js

1
推荐指数
1
解决办法
1574
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1