1

I have an array and I am trying to delete the data on basis of _id, but partial data is being pulled out and NULL value is populated in place of that.

Array

vehicle : [
{
  data :{ 
  _id:"adkabd",
  "type":"ffff"
  },
  "_id":"amklam",
  "history":[],
  "addedon":"date"
},
{
  data :{ 
  _id:"adkcbbd",
  "type":"ffff"
  },
  "_id":"amklam",
  "history":[],
  "addedon":"date"
  }]

Now I want to pull out this data

{
  data :{ 
  _id:"adkabd",
  "type":"ffff"
  },
  "_id":"amklam",
  "history":[],
  "addedon":"date"
}

completely on basis of _id:"adkabd"

Updated command :

async.series(
                [
                    function(callback) { 
                        Models.DispatchOrder.update({'vehicle.data._id': criteria._id},
                        {$unset:{'vehicle.$':""}},
                         callback()
                        );  
                    },
                    function(callback){
                        Models.DispatchOrder.update(
                        {'vehicle': null},
                        {$pull: {'vehicle':null}},
                         callback()
                        );  
                    }
                ],
                function(err,resp){
                if(err)
                    callback(err)
                callback(resp)
                }
              );

error : Trace: ERROR OCCURED [ undefined, undefined ]

Please ignore the brackets for now. Currently in output I get

data : null
"_id":"amklam",
"history":[],
"addedon":"date"

Code in the controller :

var deleteVehicle = function(params, callbackRoute){
            var projection = {};
            var options = {limit: 1};
                var criteria = {
                    _id: params._id
                };

            Services.deleteDispatchVehicle(criteria, function (err, resp) {
                if (err) {
                    callbackRoute(err)
                } else {
                    if(resp.result.n === 0) 
                        return callbackRoute("Id not found");
                        callbackRoute(null, {});
                }
            })

        }
Lovika
  • 577
  • 2
  • 10
  • 21
  • Note that in the **"normal"** case of events your `"vehicle.data._id"` would actually be a **"unique"** value. Where it is you can simply do: `Model.collection.update({'vehicle.data._id': "adkabd"},{ '$pull': { 'vehicle': { 'data._id': "adkabd" } })`. Since you are showing data where this is the same in both array elements, you [pull the first instance](https://stackoverflow.com/questions/32018889/how-to-pull-one-instance-of-an-item-in-an-array-in-mongodb) rathen than ALL matching elements. – Neil Lunn Nov 04 '17 at 01:14
  • My bad didn't notice the values of `_id`. The `vehicle.data._id` is a unique value. I am able to remove the data part with $pull but it is displaying null now and `_id, history and addedon` is still displayed – Lovika Nov 04 '17 at 02:28
  • Your update statement is incorrect. Use the one I gave you as you are "pulling" from the wrong place. The array is called `vehicle` – Neil Lunn Nov 04 '17 at 02:29
  • I have updated the question with my updated command which is failing again. I used the command which you told. – Lovika Nov 04 '17 at 04:29
  • It's failing in second callback function – Lovika Nov 04 '17 at 04:47
  • No you didn't. The code is `callback` **NOT** `callback()`. There is a big difference between "passing" a function and "calling" it. Also stop doing this on any other data than you presented in the question. Read both the linked duplicates. One shows removing a singular element out of multiple matches, and the other is a normal pull operation with syntax like I "showed you in my comment". The one in the **comment** above is the syntax for pulling a unique id value. – Neil Lunn Nov 04 '17 at 04:49
  • used the one you mentioned in the comment `Model.collection.update({'vehicle.data._id': "adkabd"},{ '$pull': { 'vehicle': { 'data._id': "adkabd" } })` and now I am getting the same value again `vehicle : { data: null "_id":"amklam", "history":[], "addedon":"date"}` – Lovika Nov 04 '17 at 05:01
  • No. That's the state of the data "as you messed it up". Start again from the original state of the data "as presented in your question". That is the question you asked, that is what is answered. That is what is a duplicate issue many times here. If you cannot work out how to fix your messed up data then [Ask a new Question](https://stackoverflow.com/questions/ask) and show your "real data" that you made a mess of. – Neil Lunn Nov 04 '17 at 05:08

1 Answers1

1

This should work

Model.collection.update({'vehicle.data._id': params._id},
                        {$pull : {vehicle: {'data._id': params._id}},
daemon24
  • 1,388
  • 1
  • 11
  • 23
  • Still getting the same output : `data: null, _id":"add",history:[],"addedon":"date"` I have updated the question with the controller part as well – Lovika Nov 03 '17 at 18:39