node.js MongoDB,从数组中删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15641492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:01:40  来源:igfitidea点击:

MongoDB, remove object from array

javascriptarraysnode.jsmongodbmongoose

提问by lostintranslation

Doc:

文件:

{
   _id: 5150a1199fac0e6910000002,
   name: 'some name,
   items: [{
      id: 23,
      name: 'item name 23'
   },{
      id: 24,
      name: 'item name 24'
   }]
}

Is there a way to pull a specific object from an array? I.E. how do I pull the entire item object with id 23 from the items array.

有没有办法从数组中提取特定对象?IE 如何从 items 数组中提取 id 为 23 的整个 item 对象。

I have tried:

我试过了:

db.mycollection.update({'_id': ObjectId("5150a1199fac0e6910000002")}, {$pull: {id: 23}});

However I am pretty sure that I am not using 'pull' correctly. From what I understand pull will pull a field from an array but not an object.

但是我很确定我没有正确使用“拉”。据我所知,pull 将从数组中拉出一个字段,而不是一个对象。

Any ideas how to pull the entire object out of the array.

任何想法如何将整个对象从数组中拉出。

As a bonus I am trying to do this in mongoose/nodejs, as well not sure if this type of thing is in the mongoose API but I could not find it.

作为奖励,我正在尝试在 mongoose/nodejs 中执行此操作,并且不确定这种类型的东西是否在 mongoose API 中,但我找不到它。

回答by sambomartin

try..

尝试..

db.mycollection.update(
    {'_id': ObjectId("5150a1199fac0e6910000002")}, 
    { $pull: { "items" : { id: 23 } } },
false,
true 
);

回答by Deepak Sisodiya

I have a document like

我有一个像

enter image description here

在此处输入图片说明

I have to delete address from address array

我必须从地址数组中删除地址

After searching lots on internet I found the solution

在互联网上搜索了很多之后,我找到了解决方案

Customer.findOneAndUpdate(query, {$pull: {address: addressId}}, function(err, data){
        if(err) {
          return res.status(500).json({'error' : 'error in deleting address'});
        }

        res.json(data);

      });

回答by Viral Patel

my database:->
        {
       "_id" : ObjectId("5806056dce046557874d3ab18"),
       "data" : [ 
           {
               "id" : 1
           }, 
           {
               "id" : 2
           }, 
           {
               "id" : 3
           }
       ]
    }

MY QUERY:->
db.getCollection('play_table').update({},{$pull:{"data":{"id":3}}},{multi:true}
OutPut:->
{
  "_id" : ObjectId("5806056dce046557874d3ab18"),
       "data" : [ 
           {
               "id" : 1
           }, 
           {
               "id" : 2
           }
       ]
    }

回答by Shubham Verma

You can try it also:

你也可以试试:

db.getCollection('docs').update({ },{'$pull':{ 'items':{'id': 3 }}},{multi:true})

回答by Chandrakesha Rao

For a single record in array:

对于数组中的单个记录:

db.getCollection('documents').update(
    { },
    {'$pull':{ 'items':{'mobile': 1234567890 }}},
    {new:true}
);

For a multiple records with same mobile number in array:

对于数组中具有相同手机号码的多条记录:

db.getCollection('documents').update(
    { },
    {'$pull':{ 'items':{'mobile': 1234567890 }}},
    {new:true,multi:true}
)

回答by KARTHIKEYAN.A

Use $pullto remove the data

使用$pull删除数据

return this.mobiledashboardModel
.update({"_id": args.dashboardId}, { $pull: {"viewData": { "_id": widgetId}}})
.exec()
.then(dashboardDoc => {
     return {
        result: dashboardDoc
     }
});