Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - Can't push an item to an array inside an object inside an array

I have this object in MongoDB:

{
  _id: ObjectId("1"),
  data: {
    city: "ccp",
    universities: [
      {
        _id: "2"
        name: "universityOne"
        students: []
      },
      {
        _id: "3"
        name: "universityTwo",
        students: []
      }
    ]
  }
}

I need to push a Student object inside the students array inside the universityOne object inside the universities array, inside the global object.

I tried documentation and come up with this queries. Mongo shell returns { "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }. And nothing changes.

Here are the queries formatted/unformatted, so you can see them:

db.pautas.updateOne({_id: ObjectId("1")}, {$push: {"data.universities.$[id].students": {name: "aStudentName", age: 22}}}, {arrayFilters: [{"id._id": ObjectId("2")}]})

db.pautas.updateOne({_id: ObjectId("1")}, {$push: {"data.universities.$[id].students": {name: "aStudentName", age: 22}}}, {arrayFilters: [{"id._id": ObjectId("2")}]})

This second query is with the name of the university on the mongo [<identifier>]. But doesn't work either.

db.pautas.updateOne({_id: ObjectId("1")}, {$push: {"data.universities.$[name].students": {name: "aStudentName", age: 22}}}, {arrayFilters: [{"name.name": "universityOne"}]})

db.pautas.updateOne({_id: ObjectId("1")}, {$push: {"data.universities.$[name].students": {name: "aStudentName", age: 22}}}, {arrayFilters: [{"name.name": "universityOne"}]})

Regards.


UPDATE

Real object:

{
  _id: ObjectId("5c6aef9bfc1a2693588827d9"),
  datosAcademicos: {
    internados: [
      { 
        _id: ObjectId("5c6bfae98857df9f668ff2eb"),
        pautas: []
      },
      {
        _id: ObjectId("5c6c140f8857df9f668ff2ec"),
        pautas: []
      }
    ]
  }
}

I need to add a Pauta to the pautas array. I've set pautas to an array of strings for debugging purpose, so just need to push a "hello world" or whatever string.

I tried this with the answers I've been given:

db.pautas.updateOne({"_id":ObjectId("5c6aef9bfc1a2693588827d9"), "datosAcademicos.internados._id": ObjectId("5c6bfae98857df9f668ff2eb")}, { $push: {"datosAcademicos.internados.$.pautas": "hi"}})

db.pautas.updateOne({"_id":ObjectId("5c6aef9bfc1a2693588827d9"), "datosAcademicos.internados._id": ObjectId("5c6bfae98857df9f668ff2eb")}, { $push: {"datosAcademicos.internados.$.pautas": "hi"}})


Update 2:

Mongo version: v4.0.2 Using Robo 3T.

I created a test database enter image description here

And tried this command enter image description here

Still not working.

like image 758
sebaLinares Avatar asked Feb 26 '19 20:02

sebaLinares


People also ask

How do you push data into an array of objects in MongoDB?

In MongoDB, the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element. If the updating field is not an array type field the operation failed.

Can we push object in array?

Example 1: Append Object to Array Using push() In the above program, the push() method is used to add an object to an array. The push() method adds an item to the end of an array.

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.

How do I pull an element from an array in MongoDB?

The $pull operator removes from an existing array all instances of a value or values that match a specified condition. The $pull operator has the form: { $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } } To specify a <field> in an embedded document or in an array, use dot notation.


1 Answers

There are 3 issues with your statement. First, the root ID field is "id" and you're querying the "_ID".

Second, you should put the match fields altogether. This update works as expected.

Third, you should use "$" to select the nested array position, not "$[id]".

db.pautas.updateOne(
{ "id": ObjectId("1"), "data.universities._id": "2"}, 
{$push: 
    {"data.universities.$.students": {name: "aStudentName", age: 22}}
})

Answer to the question UPDATE: The update statement worked just fine.

Update statement Update statement

Record updated successfuly Record after update - I ran my update with your data and then the update code you posted, both worked just fine.

like image 151
Paulo Pedroso Avatar answered Nov 02 '22 12:11

Paulo Pedroso