英文:
$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" : [ ] }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论