$pull mongodb 没有删除项目
$pull mongodb is not removing items
我有一个 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")
]
}
]
}
然后我 运行 这个 mongo shell 查询从数组中拉出一个项目并删除:
db.users.update({"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},{$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUE"}}},{ multi: true })
数组保持不变。想知道为什么这无法 $pull 吗?
我已经尝试 'ItemsToDelete.placeId' 但错误是:我在执行“ItemsToDelete.placeId”时收到的错误 => '无法使用部分(ItemsToDelete.placeId 的 ItemsToDelete)遍历元素'
您的查询没有提取任何项目,因为您在 placeId
值中缺少 o
。
> db.users.update(
{"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},
{$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUE"}}},
^
{ multi: true }
)
应该是
> 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" : [ ] }
我有一个 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")
]
}
]
}
然后我 运行 这个 mongo shell 查询从数组中拉出一个项目并删除:
db.users.update({"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},{$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUE"}}},{ multi: true })
数组保持不变。想知道为什么这无法 $pull 吗?
我已经尝试 'ItemsToDelete.placeId' 但错误是:我在执行“ItemsToDelete.placeId”时收到的错误 => '无法使用部分(ItemsToDelete.placeId 的 ItemsToDelete)遍历元素'
您的查询没有提取任何项目,因为您在 placeId
值中缺少 o
。
> db.users.update(
{"_id":ObjectId("54e664c4e7c3b4d3b5413e39")},
{$pull:{ItemsToDelete : {placeId:"ChIJIQBpAG2ahYAR_6128GcTUE"}}},
^
{ multi: true }
)
应该是
> 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" : [ ] }