Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 我可以用一个查询更新同一项中的两个数组吗?_Mongodb_Mongoose - Fatal编程技术网

Mongodb 我可以用一个查询更新同一项中的两个数组吗?

Mongodb 我可以用一个查询更新同一项中的两个数组吗?,mongodb,mongoose,Mongodb,Mongoose,我的应用程序中有组。用户可以加入并邀请其他用户。在db中,我将其存储如下: Group = { users: [UserId: ObjectId], invitations: [email: string] } 当用户接受邀请时,我希望将其从用户邀请中删除: join: (req, res, next) => { Group.findOneAndUpdate( {invitations: {$elemMatch: {email: req.user.em

我的应用程序中有组。用户可以加入并邀请其他用户。在db中,我将其存储如下:

Group = {
   users: [UserId: ObjectId],
   invitations: [email: string]
}
当用户接受邀请时,我希望将其从用户邀请中删除:

join: (req, res, next) => {
    Group.findOneAndUpdate(
        {invitations: {$elemMatch: {email: req.user.email}}},
        {$pullAll: {invitations: req.user.email}, $addToSet: {users: req.user._id}})
        .then(succ => {
            console.log(succ);
            next()
        })
        .catch(err => {
            error(res, err)
        });
},
不幸的是,这对我不起作用。是否可以通过一个查询进行两个更改?如果是,如何执行?

在参数中需要一个数组,如
$pullAll:{“邀请”:[“1”,“2”]}
。您需要指定嵌套文档
{“email”:req.user.email}
,这同样适用于
$addToSet
{“UserId”:req.user.\u id}

问题是:

Group.findOneAndUpdate({
        "invitations": {
            $elemMatch: {
                "email": req.user.email
            }
        }
    }, {
        $pullAll: {
            "invitations": [{ "email": req.user.email }]
        },
        $addToSet: {
            "users": { "UserId": req.user._id }
        }
    })
    .then(succ => {
        console.log(succ);
        next()
    })
    .catch(err => {
        error(res, err)
    });
在参数中需要一个数组,如
$pullAll:{“邀请”:[“1”,“2”]}
。您需要指定嵌套文档
{“email”:req.user.email}
,这同样适用于
$addToSet
{“UserId”:req.user.\u id}

问题是:

Group.findOneAndUpdate({
        "invitations": {
            $elemMatch: {
                "email": req.user.email
            }
        }
    }, {
        $pullAll: {
            "invitations": [{ "email": req.user.email }]
        },
        $addToSet: {
            "users": { "UserId": req.user._id }
        }
    })
    .then(succ => {
        console.log(succ);
        next()
    })
    .catch(err => {
        error(res, err)
    });

是的,您可以在一个查询中更新两个数组,您的操作几乎正确,只是一个简单的错误。您只需要从数组中提取一个元素,因此我认为您应该使用
$pull
而不是
$pullAll
,这样在您的情况下效果会更好
$pull
接受一个元素,而
$pullAll
接受一个数组,因此使用
$pull
更好

只需将
$pullAll
替换为
$pull
,它就可以工作了

试试这个:

Group.findOneAndUpdate(
    {invitations: {$elemMatch: {email: req.user.email}}},
    {$pull: {invitations: req.user.email}, $addToSet: {users: req.user._id}})
    .then(succ => {
        console.log(succ);
        next()
    })
    .catch(err => {
        error(res, err)
    });

希望这有帮助

是的,您可以在一个查询中更新两个数组,您的操作几乎正确,只是一个简单的错误。您只需要从数组中提取一个元素,因此我认为您应该使用
$pull
而不是
$pullAll
,这样在您的情况下效果会更好
$pull
接受一个元素,而
$pullAll
接受一个数组,因此使用
$pull
更好

只需将
$pullAll
替换为
$pull
,它就可以工作了

试试这个:

Group.findOneAndUpdate(
    {invitations: {$elemMatch: {email: req.user.email}}},
    {$pull: {invitations: req.user.email}, $addToSet: {users: req.user._id}})
    .then(succ => {
        console.log(succ);
        next()
    })
    .catch(err => {
        error(res, err)
    });
希望这有帮助