嘿,你如何在不删除父数组的情况下取消设置mongodb中的子数组。
以下是我使用mongoose运行的查询:
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)
}
}
}
);
但是,这会删除整个通知数组......
答案 0 :(得分:0)
您只想取消设置字段:
User.findByIdAndUpdate(
req.signedCookies.userid,
{
$addToSet: {
friend: {
friendId: mongoose.Types.ObjectId(req.body.friend),
date_added: new Date()
}
},
$unset: { 'notifications.receivedRequest': " " }
}
);