从mongodb中的数组中删除对象

时间:2016-10-25 05:32:56

标签: json mongodb

如何从以下数组集合中删除坦诚。

{
    "_id" : ObjectId("58978989"),
    "positionId" : "54535343",
    "jobTitle" : "Developer",
    "status" : "Open",
    "jobDescription" : "HyperLink Place holder",
    "candidate" : [ 
        {
            "candid" : ObjectId("b20474567892345678900021")
        }, 
        {
            "candid" : ObjectId("b30474567892345678900021")
        }, 
        {
            "candid" : ObjectId("b40474567892345678900021")
        }, 
        {
            "candid" : ObjectId("b50474567892345678900021")
        }
    ]
}

预期产出

{
    "_id" : ObjectId("58978989"),
    "positionId" : "54535343",
    "jobTitle" : "Developer",
    "status" : "Open",
    "jobDescription" : "HyperLink Place holder",
    "candidate" : [ 
        {
            "candid" : ObjectId("b20474567892345678900021")
        }, 
        {
            "candid" : ObjectId("b30474567892345678900021")
        }, 
        {
            "candid" : ObjectId("b40474567892345678900021")
        }
    ]
}

1 个答案:

答案 0 :(得分:-1)

由于数组包含相同的对象,我假设您要删除特定的索引。如果是,请使用以下内容。

db.test.update({_id: ObjectId("58978989")}, {$unset: {"candidate.1": 1}})
db.test.update( { _id: ObjectId("58978989")}, { $pull: { candidate: null} } )

看看这个以了解更多信息 https://docs.mongodb.com/manual/reference/operator/update/pull/