如何推送到对象内部的数组,MongoDB 的数组内部?

时间:2021-07-27 04:42:52

标签: javascript node.js database mongodb backend

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

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

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

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

1 个答案:

答案 0 :(得分:0)

检查这个例子

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

Mongoplayground