我有一个集合:
//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 })
阵列保持不变。有没有想过为什么这不能拉动?
我试过了' ItemsToDelete.placeId'但是出错了:我在做“ItemsToDelete.placeId”时收到的错误=> '不能使用part(ItemsToDelete of ItemsToDelete.placeId)来遍历元素'
答案 0 :(得分:2)
您查询并未提取任何项目,因为您在o
值中缺少placeId
。
> 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" : [ ] }