0

How to update active field of student 1 to true in the following mongo collection. (Given that, we know hostel_name, room_no, student_id to update)

{
  "hostel_name": "ABC",
  "rooms": [
    {
      "room_no": 1,
      "students": [
        {
          "student_id": 1,
          "active": false
        },
        {
          "student_id": 2,
          "active": true
        }
      ]
    }
  ]
}
Parth mehta
  • 1,468
  • 2
  • 23
  • 33
  • 1
    possible duplicate of [Mongodb update deeply nested subdocument](http://stackoverflow.com/questions/18173482/mongodb-update-deeply-nested-subdocument) – Neo-coder May 26 '15 at 17:08

1 Answers1

-1

ANSWER:

db.collectionname.update({"rooms.students.student_id" : 1},{"$set" : {"rooms.$.students.0.active" : true}})

{
    "_id" : ObjectId("5564fb68c8fc4ec0f15c6dd4"),
    "hostel_name" : "ABC",
    "rooms" : [ 
        {
            "room_no" : 1,
            "students" : [ 
                {
                    "student_id" : 1,
                    "active" : true
                }, 
                {
                    "student_id" : 2,
                    "active" : true
                }
            ]
        }
    ]
}
Parth mehta
  • 1,468
  • 2
  • 23
  • 33