从猫鼬等效的数组mongodb中删除一项 [英] Remove an item from an array mongodb equivalent in mongoose
问题描述
文档:
{
"_id" : ObjectId("5399ba7f8035beb2d4717cc0"),
"listName" : "firstList",
"items" : [
{
"pName" : "iPad ",
"pDesc" : "iPad aiiir",
"ownerID" : ObjectId("5399b596c501732dd4d13923"),
"_id" : ObjectId("5399e2cc2d7b0349d89d2b44"),
"dateAdded" : ISODate("2014-06-12T17:26:36.282Z")
},
{
"pName" : "iPhone ",
"pDesc" : "5s",
"ownerID" : ObjectId("5399b596c501732dd4d13923"),
"_id" : ObjectId("5399e2cc2d7b0349d89d2b44"),
"dateAdded" : ISODate("2014-06-12T17:26:36.282Z")
}
]}
我想从数组"项中删除一项,不知道文档的_id仅该元素的ID, 我有这个查询,可以找到元素,它在mongodb shell中有效,但在mongoose中无效,
I want to remove an item from the "array" items without knowing the _id of the document only the id of the element, I have this query that finds the element, it works in mongodb shell but doesn't in mongoose,
db.lists.find( {items:
{$elemMatch:{'_id':ObjectId("5399b596c501732dd4d13923")}}} ,
{"items.$":1});
您知道为什么吗?
你能给我等价的猫鼬吗?
Can you please give me the equivalent in mongoose ?
我应该如何从仅知道其_id的列表中拉出项目?
How should I pull the item from the list knowing only its _id ?
推荐答案
这应该可以解决问题:
db.lists.update(
{},
{ $pull: {"items": {"_id" :ObjectId("5399e2cc2d7b0349d89d2b44") }},
{ multi: true }})
对于集合中的每个文档({}
表示空查询),它将删除具有提供的_id
(在items
数组中)的每个项目.如果没有multi
选项,则只会影响找到的第一个文档
For every document in the collection ({}
means empty query), it removes every item with the provided _id
(in the items
array). Without the multi
option, only the first document found will be affected
这篇关于从猫鼬等效的数组mongodb中删除一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!