0

I have a question when trying to remove a sub-document from MongoDB.

Here is my data structure:

"event" : {
  "_id": "581fec8011acfb28f6bb0b96",
  "participants": [
  {
    "_id": "582041475aa37c0e27fde28b",
    "regDate": "2016-11-07T08:54:31.155Z",
    "group": "board member",
    "lastName": "1",
    "firstName": "test",
    "eventCode": "07e05d"
  },
  {
    "_id": "5820414b5aa37c0e27fde28c",
    "regDate": "2016-11-07T08:54:35.462Z",
    "group": "participant",
    "lastName": "2",
    "firstName": "test",
    "eventCode": "f1685a"
  },
  {
    "_id": "5820414e5aa37c0e27fde28d",
    "regDate": "2016-11-07T08:54:38.441Z",
    "group": "participant",
    "lastName": "3",
    "firstName": "test",
    "eventCode": "a71ec5"
  }
]
} 

I tried to use lodash to delete one participant but lodash remove all of my participants. Can you help me to solve this problem?

I updated the whole code at server side where has problem

var alias = req.params.alias;
var pid = req.params.pid; 

    var findAndRemoveParticipant = new Promise ((resolve, reject) => {
        Event.findOne({
            "alias":alias
        },'participants')
        .exec((err, event) => {
            if(err) reject(err)
            else {
                _.remove(event.participants, {_id: pid});
                event.save((err, saved) => {
                    err ? reject(err) : resolve(saved)
                })
            }    
        })
    });

    findAndRemoveParticipant
        .then(saved => {
            res.status(200).json('Participant removed successfully');
        })
        .catch(err => res.status(400).json(err));

In client side I use the same code, and it work

$http.delete(_deleteParApi)
        .success(msg => {
            $scope.parRemoved = msg;
            _.remove($scope.participants,{_id: pid})
        })  
        .catch(err => {
            console.log(err);
        })
Giang Đỗ Hoàng
  • 137
  • 1
  • 1
  • 8

3 Answers3

0

Do this

MODEL.find(YOUR QUERY).lean().exec(function(err, data){
// MODIFY DATA NOW AND SAVE IT
})

DUPLICATE QUESTION


OR

Mongoose Models inherit from Documents, which have a toObject() method. I believe what you're looking for should be the result of doc.toObject().

take a look

var pid = req.params.pid;/
event.toObject()
event.participants = _.remove(event.participants,{_id: pid})
console.log(event.participants);
// []
event.save((err,saved) => {
    err ? reject(err) : resolve(saved)
}
Community
  • 1
  • 1
Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28
0

According to the lodash document, it removes the matched items from the array and returns an array of the removed elements, so you don't need to assign it back to event.participants but if you do, you need to check for 'not equal' to get the returned array you want. Here is the revised code:

var pid = req.params.pid;
event.participants = _.remove(event.participants,function(o) {
  return o._id !== pid;
});
console.log(event.participants);

Here is the output if pid = '582041475aa37c0e27fde28b':

[{ 
    _id: '5820414b5aa37c0e27fde28c',
    regDate: '2016-11-07T08:54:35.462Z',
    group: 'participant',
    lastName: '2',
    firstName: 'test',
    eventCode: 'f1685a' 
}, { 
    _id: '5820414e5aa37c0e27fde28d',
    regDate: '2016-11-07T08:54:38.441Z',
    group: 'participant',
    lastName: '3',
    firstName: 'test',
    eventCode: 'a71ec5' 
}]
Ben
  • 5,024
  • 2
  • 18
  • 23
0

use mongo $pull :

 Event.update(
    {"alias":alias},
    { $pull: { 'participants': { _id: pid } } }
);
Daphoque
  • 4,421
  • 1
  • 20
  • 31