0

親配列を削除せずに、mongodb でサブ配列を $unset する方法を教えてください。

これは、マングースを使用して実行しているクエリです。

User.findByIdAndUpdate(
    req.signedCookies.userid,
    {
        $addToSet: {
            friend: { 
                friendId: mongoose.Types.ObjectId(req.body.friend),
                date_added: new Date()
            }
        },
        $unset: {
            notifications: {
                receivedRequest: mongoose.Types.ObjectId(req.signedCookies.userid)
            }
        }
    }
);

ただし、これにより通知配列全体が削除されます...

4

1 に答える 1

0

フィールドのみを設定解除したい:

User.findByIdAndUpdate(
    req.signedCookies.userid,
    {
        $addToSet: {
            friend: {
                friendId: mongoose.Types.ObjectId(req.body.friend),
                date_added: new Date()
            }
        },
        $unset: { 'notifications.receivedRequest': " " }
    }
);
于 2013-07-24T17:20:17.903 に答える