【问题标题】:How do you push to an array inside of an object, inside of an array with MongoDB?你如何推送到对象内部的数组,在 MongoDB 的数组内部?
【发布时间】:2021-07-27 13:18:17
【问题描述】:

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

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

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

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

【问题讨论】:

标签: javascript node.js database mongodb backend


【解决方案1】:

检查这个例子

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

Mongoplayground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2015-09-11
    • 2017-09-26
    • 1970-01-01
    • 2022-11-25
    • 2015-11-14
    相关资源
    最近更新 更多