我在MongoDB中有一个集合。 并想从阵列中移除一个项目。
我的“用户”集合是一组对象。
当我键入:
db.users.find({"tasks.task_id" : "h58sjIdj3jJZ"}).pretty()
在蒙戈外壳,我得到这样的结果:
{
"_id" : ObjectId("5955b45b7a4bf40544019359"),
"profile" : {
"name" : "Morning bay",
"email" : "[email protected]",
"phone" : "+1-641-155-88-84",
"description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
},
"tasks" : [
{
"task_id" : "h58sjIdj3jJY",
"time" : "11:15 AM",
"date" : "07/01/2017",
"description" : "Make 1st call to John White"
},
{
"task_id" : "h58sjIdj3jJZ",
"time" : "14:30 PM",
"date" : "07/09/2017",
"description" : "Send certificate and make Another call to J.White"
}
],
"progress" : [
{
"isActive" : "",
"description" : ""
}
]
}
所以,我的收藏中的每一项看起来是这样的。 没关系。
但是现在我想从一个用户项目中的“tasks”数组中删除一个项目。
I型:
db.users.update({}, {$pull : {"tasks" : {"task_id" : "h58sjIdj3jJZ"}}});
的得到这样的结果:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
我阅读文档和示例,它看起来很相似,所以我不明白的地方是一个错误。
它应该工作。你的mongodb版本是什么? –
你可以添加{multi:true}作为更新的第三个参数吗? 'db.users.update({},{$ pull:{“tasks”:{“task_id”:“h58sjIdj3jJZ”}}},{multi:true});' –
已经尝试过了,但并没有帮助 – alexfrize