Mongodb从记录中删除所有地址

时间:2018-02-19 05:41:59

标签: mongodb

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

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

])

1 个答案:

答案 0 :(得分:3)

您可以使用$unset运算符

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

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