我是Node.js的新手。 我有一些这样的数据
我想更新此歌曲对象并删除歌手“ hanna” ..以便最终使歌曲中没有歌手。
我尝试了很多方法,其中一些在这里被评论
这是我的代码:
var data = req.body;
let songCondition = {
"artist.$.id": data._id,
}
let updateSong = {
// $pull: { "artist": { "id": data._id } }
// $pull: { "artist": { "id": { $in: [data._id] } } }
$pull: { artist: { $elemMatch: { id: data._id } } }
}
let updateSongData = await Query.findAndUpdate(Song, songCondition, updateSong);
有人可以建议我在这里做错什么吗?
答案 0 :(得分:2)
在findOneAndUpdate
函数中,第一个参数用于filter
,第二个参数用于update
const { _id } = req.body
Song.findAndUpdate(
{ "songs.artist": { "$elemMatch": { id: _id }}}, // first parameter is for fitler
{ "$pull": { "artist": { "id": _id } } } // second one for update
)