Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
Mongo DB Push to Array of Objects?

Working on a project that uses MongoDB as a database. I'm stuck on trying to get an array of objects I have to update. Here is the schema:

roundData : {
                playsArray : [ 
                    {
                        player1 : {
                            card1 : 1,
                            card2 : 2
                        },
                        player2 : {
                            card1 : 0,
                            card2 : 0
                        }
                    },
                ],

So basically I want a single array that holds an object within that object are two objects.

Plays Array [ object1, object2] where object 1 would be {player 1 : {card1 : 0, card2 : 0}}, {player 2 : {card1 : 0, card2 : 0}}.

Every time I try to write an updateOne function, it at best only updates player 1 or it seperates player 1 and player 2 as two items in the array. Any suggestions what I'm doing wrong on the formating?

Here's my current code:

const gameData = await GameData.updateOne({ gameId: gameIdInput }, { $push: { "matchData.roundData.playsArray": { $each : [ {"player1" : {"card1" : 0, "card2" :0} }, {"player2" : {"card1" : 0, "card2" :0} } ] } } } );

This works, but it pushes player 1 and player 2 into the array separately so that the array becomes [ player 1/2 objects, player 1 objects, player 2 objects ].

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Nevermind! I was missing [ ] around the object in a previous version without $each. Leaving here for solution if anyone finds it helpful.

const gameData = await GameData.updateOne({ gameId: gameIdInput }, { $push: { "matchData.roundData.playsArray": [ {"player1" : {"card1" : 0, "card2" :0} }, {"player2" : {"card1" : 0, "card2" :0} } ]  } } );

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda