MongoDB, remove object from array

前端 未结 7 2086
余生分开走
余生分开走 2020-11-22 16:19

Doc:

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


        
相关标签:
7条回答
  • 2020-11-22 16:55
    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
               }
           ]
        }
    
    0 讨论(0)
  • 2020-11-22 17:02

    I have a document like

    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);
    
          });
    
    0 讨论(0)
  • 2020-11-22 17:02

    You can try it also:

    db.getCollection('docs').update({ },{'$pull':{ 'items':{'id': 3 }}},{multi:true})
    
    0 讨论(0)
  • 2020-11-22 17:02

    Use $pull to remove the data

    return this.mobiledashboardModel
    .update({"_id": args.dashboardId}, { $pull: {"viewData": { "_id": widgetId}}})
    .exec()
    .then(dashboardDoc => {
         return {
            result: dashboardDoc
         }
    });
    
    0 讨论(0)
  • 2020-11-22 17:14

    try..

    db.mycollection.update(
        {'_id': ObjectId("5150a1199fac0e6910000002")}, 
        { $pull: { "items" : { id: 23 } } },
    false,
    true 
    );
    
    0 讨论(0)
  • 2020-11-22 17:15

    Kishore Diyyana:

    If you want to remove all elements including key of the element attributes list. Here is the example of mongoDB unset operator:

    db.UM_PREAUTH_CASE.update( { 'Id' : 123}, { $unset: { dataElements: ""} } )

    JSON Look like this:

    { "Id":123,"dataElements" : [ { "createdBy" : "Kishore Babu Diyyana", "createdByUserId" : 2020 }, { "createdBy" : "Diyyana Kishore", "createdByUserId" : 2021 } ] }

    0 讨论(0)
提交回复
热议问题