没有删除embadded文件mongo

时间:2018-02-18 14:03:12

标签: mongodb pull

我有这些数据:

db.school.find({})

{ 
    "_id" : ObjectId("5a897f6c49bf66c10a70ed0d"), 
    "first_name" : "Jaskaran", 
    "last_name" : "singh", 
    "age" : 30.0
}
{ 
    "_id" : ObjectId("5a897f6c49bf66c10a70ed0f"), 
    "first_name" : "Amrit", 
    "last_name" : "singh", 
    "age" : 31.0
}
{ 
    "_id" : ObjectId("5a897f6c49bf66c10a70ed0e"), 
    "first_name" : "Vikas", 
    "last_name" : "Sharma", 
    "age" : 31.0, 
    "Address" : {
        "Pincode" : 1234.0
    }
}

我想删除密码

$ pull不起作用!!

db.school.update(
{
    '_id':ObjectId("5a897f6c49bf66c10a70ed0e")
},
{ $pull: {
    Address: {
        Pincode:1    }    
} },
{ multi:true }
);

WriteResult({“nMatched”:1,“nUpserted”:0,“nModified”:0})

db.school.update({}, {$pull:{ "Address": {$elemMatch: {Pincode :1234 } }}})

1 个答案:

答案 0 :(得分:3)

$pull用于数组,而您尝试操作子文档。

这样做的方法是使用$unset

db.school.update({"Address.Pincode" : 1234}, { $unset: { "Address.Pincode" : "" } })

代码1234.要删除每个密码:

db.school.update({}, { $unset: { "Address.Pincode" : "" } })