Currently, I am facing a challenge in my project which involves utilizing MongoDB as the database. Specifically, I am struggling with updating an array of objects within the schema outlined below:
roundData : {
playsArray : [
{
player1 : {
card1 : 1,
card2 : 2
},
player2 : {
card1 : 0,
card2 : 0
}
},
],
My goal is to have a single array containing objects, and each of these objects should consist of two nested objects.
In essence, I am looking to have Plays Array [ object1, object2] where object 1 would be {player 1 : {card1 : 0, card2 : 0}}, {player 2 : {card1 : 0, card2 : 0}}.
Despite my attempts to write an updateOne function, I am encountering issues where it only updates player 1 or splits player 1 and player 2 as separate items in the array. Can you provide any insights into what might be causing this formatting error?
For reference, here is the code snippet I am currently using:
const gameData = await GameData.updateOne({ gameId: gameIdInput }, { $push: { "matchData.roundData.playsArray": { $each : [ {"player1" : {"card1" : 0, "card2" :0} }, {"player2" : {"card1" : 0, "card2" :0} } ] } } } );
While the code executes successfully, the issue lies in how it pushes player 1 and player 2 separately into the array, resulting in a structure like [ player 1/2 objects, player 1 objects, player 2 objects ].