0

I am a newcomer in mongoDB and this is trying to add an element to an array using push, when I try on the arrays progreso or data_pjs there is no problem I have the problem when I try to insert it on the array datos_progreso

/* 1 */ {
    "_id" : ObjectId("5a2a99935887b3f20e1294c6"),
    "__v" : 0,
    "player" : “Player1”,
    "gremio" : "27866",
    "progreso" : [ 
        {
            "gPowerC" : "2,592,335",
            "gPowerS" : "1,594,634",
            "fecha" : ISODate("2017-12-07T17:00:21.000Z"),
            "_id" : ObjectId("5a2a99935887b3f20e1294c7")
        }, 
        {
            "gPowerC" : "5,592,335",
            "gPowerS" : "5,594,634",
            "fecha" : ISODate("2017-12-10T17:00:21.000Z"),
            "_id" : ObjectId("5a2aa12cc24daa1417b35107")
        }
    ],
    "datos_pjs" : [ 
        {
            "name" : "R2-D2",
            "_id" : ObjectId("5a2a99935887b3f20e1295cc"),
            "datos_progreso" : [ 
                {
                    "level" : "85",
                    "gear" : "XII",
                    "power" : "19,873",
                    "star" : "7",
                    "fecha" : ISODate("2017-12-07T17:00:21.000Z"),
                    "_id" : ObjectId("5a2a99935887b3f20e1295cd")
                },
                {
                  /******   HERE ******/
                }
            ]
        }, 
        {
            "name" : "Jyn Erso",
            "_id" : ObjectId("5a2a99935887b3f20e1295ca"),
            "datos_progreso" : [ 
                {
                    "level" : "85",
                    "gear" : "XII",
                    "power" : "19,873",
                    "star" : "7",
                    "fecha" : ISODate("2017-12-07T17:00:21.000Z"),
                    "_id" : ObjectId("5a2a99935887b3f20e1295cb")
                }
            ]
        } 
    ] }

I try to add the element using this command but it does not work, I would push the element in the data_progress matrix in the R2-D2 character and at the same time push REY element to data_pjs, REY push if that works for me. Can the two operations be performed at the same time?

db.getCollection('pruebas').update ( 
    { "_id" : ObjectId("5a2a99935887b3f20e1294c6") },
    {     
        $push: {    
            "datos_pjs" :  {
                "name" : "R2-D2",
                "_id" : ObjectId("5a2a99935887b3f20e1295cc"),
                "datos_progreso" : {
                    "level" : "87",
                    "gear" : "XII",
                    "power" : "9,873",
                    "star" : "5",
                    "fecha" : ISODate("2017-12-07T17:00:21.000Z"),
                    "_id" : ObjectId()
                }  
            },
            "datos_pjs" :  {
                "name" : "Rey",
                "_id" : ObjectId(),
                "datos_progreso" : {
                    "level" : "85",
                    "gear" : "XII",
                    "power" : "9,873",
                    "star" : "5",
                    "fecha" : ISODate("2017-12-07T17:00:21.000Z"),
                    "_id" : ObjectId()
                }  
            }
        }
    }  
)

can anybody help me?

  • You need a positional operator to locate the `datos_pjs` array based on the query condition (`"datos_pjs.name":"R2-D2"`) and then use the `$push` to insert the element `datos_progreso` using `$` placeholder at the specified position in `datos_pjs` found from query conditionr. – s7vr Dec 08 '17 at 15:36
  • Possible duplicate of [Mongodb $push in nested array](https://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array) – s7vr Dec 08 '17 at 15:42
  • I can not do that because I try to insert at the same time in the array of datos_pjs and in datos_progreso. I put another code in which if I work the insertion of datos_pjs but not in datos_progreso – Javier Marcellan Dec 08 '17 at 16:48
  • I don't think you can do it at the same time. First operation requires you to locate the `datos_pjs` array where you would add a new array element into `datos_progreso` and second operation is just adding a new array element into `datos_pjs`. You can use bulk update to do it both in the same server call but really should be done in two separate calls. – s7vr Dec 08 '17 at 16:59
  • How would an example of bulk.update () be with push for this case? – Javier Marcellan Dec 08 '17 at 18:07
  • Something like `var bulk = db.collection_name.initializeUnorderedBulkOp(); bulk.update( first ); bulk.update( second ); bulk.execute( );` More [here](https://docs.mongodb.com/manual/reference/method/js-bulk/) – s7vr Dec 08 '17 at 19:07

0 Answers0