为什么一次更新中的两个 $push 在 mongodb 中不起作用

Why two $push inside one update is not working in mongodb

当我分别更新两个数组时,它工作正常。但是当我一起更新时,只有第二个数组在更新

var selection = {
    _id : {$in : membersIds}
};

var update = {
    $push : {
      notifications_Array_Object : newNotification
    },
    $push : {
      groups_Array_Object : ObjectID(groupId)
    },
    $inc : {unreadNotifications_Integer : 1}
};
userDb.update(selection,update,{multi : true}, function (error,result) {
    ...
});

当 userDb.update() 分别用于 notifications_Array_Object 和 groups_Array_Object 时,它的工作正常。

您必须更改查询

发件人:

var update = {
    $push : {
      notifications_Array_Object : newNotification
    },
    $push : {
      groups_Array_Object : ObjectID(groupId)
    },
    $inc : {unreadNotifications_Integer : 1}
};

收件人:

var update = {
    $push : {
      notifications_Array_Object : newNotification, groups_Array_Object : ObjectID(groupId)
    }
    $inc : {unreadNotifications_Integer : 1}
};