Problem with $push

I can’t add an object containing a property associated with an array of objects to mongoDB, below I put my code, I’ve already used $push elsewhere and it worked, but here it doesn’t work, it returns the matchedCount: 1 and that the modifiedCount: 0.

let obj = { "ordine": [...data.order] };

result = await restaurantsCollection.updateOne(
                    { 'nameRestaurant': data.nameRestaurant },
                    {
                        $push: { 'restaurant.$[elem].tavolo.temp.$[elem2].ordini': obj }
                    },
                    {
                        arrayFilters: [
                            { 'elem.tavolo.numTavolo': data.numTavolo },
                            { 'elem2.active': true }
                        ]
                    }
                );

Hello @Samuele_Cervietti, Welcome Back,

It would be easy to understand your problem if you could provide more information:

  • an example document/schema
  • input values
  • expected behavior and result
1 Like

This is the pattern of my collection:

{
    "restaurant":[
        {
            "tavolo":{
                "numTavolo":1,
                "temp":[
                    {
                        "id":1213876346,
                        "active":true,
                        "ordini":[]
                    }
                    ]
            }
        },
        {
            "tavolo":{
                "numTavolo":2,
                "temp":[
                    {
                        "id":1213812346,
                        "active":true,
                        "ordini":[]
                    }
                    ]
            }
        },
        ]{
            "tavolo":{
                "numTavolo":3,
                "temp":[
                    {
                        "id":1213876326,
                        "active":true,
                        "ordini":[]
                    }
                    ]
            }
        }
}

The input value is:

obj={ "ordine" : [ {"name" : 'Paolo' }, {"name" : 'Marco' }, {"name" : 'Luca' } ] }

The expected result is, as can be seen in the first case of numTavolo=1, an object with ordine properties of the array type is inserted into the ordini array, which in turn contains a series of objects. I put the result below:

{
    "restaurant":[
        {
            "tavolo":{
                "numTavolo":1,
                "temp":[
                    {
                        "id":1213876346,
                        "active":true,
                        "ordini":[
                            { 
                                "ordine" : [ 
                                    {"name" : 'Paolo' }, 
                                    {"name" : 'Marco' }, 
                                    {"name" : 'Luca' } 
                                    ] 
                                
                            }
                            ]
                    }
                    ]
            }
        },
        {
            "tavolo":{
                "numTavolo":2,
                "temp":[
                    {
                        "id":1213812346,
                        "active":true,
                        "ordini":[]
                    }
                    ]
            }
        },
        ]{
            "tavolo":{
                "numTavolo":3,
                "temp":[
                    {
                        "id":1213876326,
                        "active":true,
                        "ordini":[]
                    }
                    ]
            }
        }
}

Hello @Samuele_Cervietti,

The query looks good, it is working for me,

As you are saying it matches the document but is not modified, Probably the issue must be in input data.numTavolo inside arrayFilters.

Try executing the query by specifying static values and see what happens,

result = await restaurantsCollection.updateOne(
  { "nameRestaurant": "abc" },
  {
    "$push": {
      "restaurant.$[elem].tavolo.temp.$[elem2].ordini": { 
        "ordine" : [ {"name" : 'Paolo' }, {"name" : 'Marco' }, {"name" : 'Luca' } ] 
      }
    }
  },
  {
    "arrayFilters": [
      { "elem.tavolo.numTavolo": 1 },
      { "elem2.active": true }
    ]
  }
)

See your working query,

1 Like