sebaLinares
sebaLinares

Reputation: 495

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.

Upvotes: 9

Views: 3953

Answers (1)

Paulo Pedroso
Paulo Pedroso

Reputation: 3705

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.

Upvotes: 8

Related Questions