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"}})