如何推送到 MongoDB 中的內部陣列?
[
{ emoji1: ['user19', 'user20', 'user21']},
{ emoji5: ['user12', 'user13', 'user14']},
{ emoji9: ['user29', 'user30', 'user34']}
]
我試過:
await Post
.updateOne({ _id: postID }, {
$push: {
[`reactions[0].emoji1`]: 'random user'
}
})
...其中 Post 是我的 Mongoose Schema,而“reactions”是上面的陣列。我想我在 $push 中做錯了什么。
結果應該是:
[
{ emoji1: ['user19', 'user20', 'user21', 'random user']},
{ emoji5: ['user12', 'user13', 'user14']},
{ emoji9: ['user29', 'user30', 'user34']}
]
提前致謝!
uj5u.com熱心網友回復:
您可以使用此查詢:
db.collection.update({
id: 1,
"reactions.emoji1": {
"$exists": true
}
},
{
"$push": {
"reactions.$.emoji1": "random user"
}
})
示例在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350904.html
