【问题标题】:how to insert an object into the players array in mongoDB?如何将对象插入到 mongoDB 中的玩家数组中?
【发布时间】:2021-09-03 01:08:29
【问题描述】:

我有下面的文档,我需要将一个对象插入到播放器数组中如何使用 mongoDB 执行此操作

{
        "data": {
            "createTournament": {
                "_id": "6130d9a565aa744f173a824a",
                "title": "Jogo de truco",
                "description": "",
                "status": "PENDING",
                "size": 8,
                "prizePool": 20,
                "currency": "USD",
                "type": "Battle",
                "entryFee": 1,
                "startDate": "2021-09-01",
                "endDate": "2021-09-01",
                "rounds": [{
                    "round": 1,
                    "totalMatches": 4,
                    "matches": [{
                            "match": 1,
                            "players": []
                        }
                    ]
                }]
            }
        }
    }

【问题讨论】:

标签: mongodb mongoose


【解决方案1】:

它会将3添加到数组matches的匹配为1且rounds数组的匹配为1的数组玩家

db.collection('exmaple').updateOne({},
{
      $push:{"data.createTournament.rounds.$[outer].matches.$[inner].players":"3"}
    },
    { "arrayFilters": [
      { "outer.round": 1 }, // you could change this to choose in which array must be pushed
      { "inner.match":1  } // // you could change this to choose in which array must be pushed
  ] }
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 2021-10-25
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    相关资源
    最近更新 更多