9

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.

4
  • 1
    Is it _id or id on root level of your document ? If you change the filtering condition from id to _id it works Commented Feb 26, 2019 at 20:45
  • It was _id, i edited it. Thanks for your answer. But it still doesn't work. Commented Feb 26, 2019 at 21:13
  • 1
    May be collection name example vs pautas ? Commented Feb 28, 2019 at 1:20
  • Jesus, you are right. Commented Feb 28, 2019 at 2:43

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

8 Comments

What's the error you're getting when running it? In my tests I removed the ObjectId("1") and used only "1". This was causing some error here.
No error, same { "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }. My object is not exactly the same but it's the same structure, I don't know what's wrong. I tried removing every ObjectId or only the first or last one, but same thing happened.
So the problem is in the match sentence. If you post your real object and what you wanna match I can give you a hint.
I updated the answer with some screenshots. The update statement worked just fine here. I am using Robot3T to operate the database. Where are you trying to update those records from? Code? Another interface? What's the Mongo version you're using?
I just realized that my query had the name of another collection, not the one I was trying to update. Thank you very much for your answers and most of all, your time. This is the correct answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.