你如何推到一个对象内部的数组,与MongoDB的数组内部?



我需要能够将新对象推送到fameTotals。遇到一点麻烦,谢谢!

这是文档在我的数据库中的样子:

{
clanName: null,
clanTag: null,
players: [
{
name: null,
tag: null,
fameTotals: [ //PUSH TO THIS ARRAY
{fame: 0, clanTrophies: 0, date: ''}
]
}
]
}

我一直在尝试用updateOne()$push运算符来解决这个问题。

检查这个例子

db.collection.update({
_id: 1
},
{
$push: {
players: {
name: "yyy",
tag: "bbbb",
fameTotals: [
{
fame: 1,
clanTrophies: 1,
date: "27-7-21"
}
]
}
}
})

Mongoplayground

最新更新