Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 知乎专栏 | 视频教程 | About

45.14. 内嵌对象

45.14.1. Array / List 列表类型

			
db.foo.save(
{
	'name':'neo',
	'address':[{'city':'shenzhen','post':"518000"}, {'city':'heilongjiang','post':"135000"}],
	'phone':["13113668800","13322993040","13266884444"]
}
)
			
			
			
{
    "_id" : ObjectId("5bbefed1b04a8c0d7395d1b5"),
    "name" : "neo",
    "address" : [ 
        {
            "city" : "shenzhen",
            "post" : "518000"
        }, 
        {
            "city" : "heilongjiang",
            "post" : "135000"
        }
    ],
    "phone" : [ 
        "13113668800", 
        "13322993040", 
        "13266884444"
    ]
}	
			
			

删除数组元素

			
db.getCollection('foo').update({"_id":ObjectId("5bbeff40b04a8c0d7395d1b6")},{"$pull":{"phone":"13266884444"}})		
			
			

删除数组中的对象

			
db.getCollection('foo').update({"_id":ObjectId("5bbeff40b04a8c0d7395d1b6")},{"$pull":{"address":{"city":"heilongjiang"}}})
			
			

查找替换

			
db.getCollection('foo').update({"address.city":"shenzhen"},{"$set":{"address.$.post":"000000"}})