我有一个集合://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)遍历元素”
1 回答
天涯尽头无女友
TA贡献1831条经验 获得超9个赞
您查询,因为你缺少不拉任何项目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" : [ ] }
- 1 回答
- 0 关注
- 182 浏览
添加回答
举报
0/150
提交
取消