假如說咱們這個集合裏面的文檔都是這種格式:ide
{ "_id" : ObjectId("5c177b84f4e84940e0bd578d"), "name" : "suhaozhi", "pwd" : "suhaozhi", "phone" : [ { "phone_num" : "13333333333", "type" : "cmcc" }, { "phone_num" : 15555555555, "type" : "chinanet" } ], "type" : "chinanet" }spa
如今須要搜索出phone字段中type子字段的值爲CMCC的記錄:文檔
db.tmp.find({phone:{$elemMatch:{cs:"cmcc"}}});it
若是要修改掉上面這個文檔phone字段中phone_num 爲 13333333333 對應的 type字段 爲chinanet:class
db.tmp.update({"name":"suhaozhi","phone.phone_num":"13333333333"},{$set:{"phone.$.cs":"chinanet"}});date
若是想在內嵌文檔中,再插入一條數據:搜索
db.tmp.update({"name":"suhaozhi"},{$push:{phone:{"phone_num" : "23333", "type" : "cmcc"}}})數據
從內嵌文檔中刪除一條數據:
db