繁体   English   中英

Mongodb从记录中删除所有地址

[英]Mongodb remove all the Address from records

我想从这些记录中删除所有地址对象。 在某些记录中,地址对象不存在。

db.name.insert([ 
    {  "_id" : ObjectId("5a898f6c49bf66c10a70ed0d"),  "first_name" : "Sandeep",  "last_name" : "kumar",  "age" : 30.0 },
    {  "_id" : ObjectId("5a863f6c49bf66c10a70ed0f"),  "first_name" : "Dev",  "last_name" : "singh",  "age" : 31.0 },
    {  "_id" : ObjectId("5a878f6c49bf66c10a70ed0e"),  "first_name" : "Ram",  "last_name" : "Sharma",  "age" : 31.0,  "Address" : { "Pincode" : 1234.0 }
    },
    {  "_id" : ObjectId("5a864f6c49bf66c10a70ed0e"),  "first_name" : "him",  "last_name" : "Sharma",  "age" : 31.0,  "Address" : { "Pincode" : 45787 } },
    {  "_id" : ObjectId("5a864f6c49bf66c10a70ed0e"),  "first_name" : "him",  "last_name" : "Sharma",  "age" : 31.0,  "Address" : { "Pincode" : 7899, "City" : "Delhi" } }

])

您可以使用$ unset运算符

db.name.update({}, { $unset: { "Address": 1 } }, { multi: true })

您希望所有文档都受到影响,因此您应指定multi: true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM