0

I have the document below and I need to insert an object into the players arrays how to do this with 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": []
                        }
                    ]
                }]
            }
        }
    }
  • First step: figure out how to do it via the shell. See https://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array and https://stackoverflow.com/questions/36113069/insert-element-into-nested-arrays-in-mongodb – Paul Sep 02 '21 at 16:44

1 Answers1

0

it will add 3 to array players that array matches has match of 1 and rounds array has round of 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
  ] }
)
mohammad Naimi
  • 2,259
  • 1
  • 5
  • 15