2015-05-26 36 views
0

如何更新活動學生1的字段到true在下面的mongo集合中。 (鑑於此,我們知道hostel_name,room_no,student_id數據更新)更新MongoDB中的二級數組元素

{ 
    "hostel_name": "ABC", 
    "rooms": [ 
    { 
     "room_no": 1, 
     "students": [ 
     { 
      "student_id": 1, 
      "active": false 
     }, 
     { 
      "student_id": 2, 
      "active": true 
     } 
     ] 
    } 
    ] 
} 
+1

可能重複(http://stackoverflow.com/questions/ 18173482/mongodb的更新-深深嵌套-子文檔) – Yogesh

回答

-1

答:中[MongoDB的更新深度嵌套子文檔]

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 
       } 
      ] 
     } 
    ] 
}