$pull mongodb 不会删除项目

huangapple go评论99阅读模式
英文:

$pull mongodb is not removing items

问题

我有一个集合:

//the json.
{
    "ItemsToDelete" : [ 
        {
            "placeId" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
            "users" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "placeId" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
            "users" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "placeId" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
            "users" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }
    ]
}  

{
    "ItemsToRemove" : [ 
        {
            "ChIJIQBpAG2ahYAR_6128GcTUEo" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "ChIJIQBpAG2ahYAR_6128GcTUEo" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "ChIJIQBpAG2ahYAR_6128GcTUEo" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }
    ]
}

然后我运行这个mongo shell查询来删除数组中的一个项目:

db.users.update(
    {"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},
    {$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUE"}}},
    {multi: true}
)

数组仍然没有被修改。有什么想法为什么无法成功使用$pull?

我尝试过使用'ItemsToDelete.placeId',但是会出现错误:当执行"ItemsToDelete.placeId"时收到的错误消息为'cannot use the part (ItemsToDelete of ItemsToDelete.placeId) to traverse the element'。

英文:

I have a collection:

//the json.
{
"ItemsToDelete" : [ 
        {
            "placeId" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
            "users" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "placeId" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
            "users" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "placeId" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
            "users" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }
    ]
}  


{
"ItemsToRemove" : [ 
        {
            "ChIJIQBpAG2ahYAR_6128GcTUEo" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "ChIJIQBpAG2ahYAR_6128GcTUEo" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }, 
        {
            "ChIJIQBpAG2ahYAR_6128GcTUEo" : [ 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15"), 
                ObjectId("547e4650f3fb0a022110af15")
            ]
        }
    ]
}

I then run this mongo shell query to pull an item off the array and delete:

db.users.update({"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},{$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUE"}}},{ multi: true })

The array remains unmodified. Any thoughts why this is failing to $pull?

I have tried 'ItemsToDelete.placeId' but that errors out with: error I receive when doing “ItemsToDelete.placeId” => ‘cannot use the part (ItemsToDelete of ItemsToDelete.placeId) to traverse the element’

答案1

得分: 2

你的查询没有返回任何项,因为在placeId的值中缺少了o

应该是这样的:

> db.users.update(
    {"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},
    {$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUEo"}}},
    { multi: true }
  )

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

> db.users.find().pretty()
{ "_id" : ObjectId("54eac8d51a0c74438c9dea10"), "ItemsToDelete" : [ ] }
英文:

You query doesn't pull any item because you are missing o in placeId value.

> db.users.update(
    {"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},
    {$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUE"}}},
                                                                ^
    { multi: true }
  )

Should be

> db.users.update(
    {"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},
    {$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUEo"}}},
    { multi: true }
  )

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

> db.users.find().pretty()
{ "_id" : ObjectId("54eac8d51a0c74438c9dea10"), "ItemsToDelete" : [ ] }

huangapple
  • 本文由 发表于 2015年2月23日 05:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/28663273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定