lodash从数组中删除所有对象。不遵循我的规范

时间:2016-11-07 09:02:38

标签: node.js lodash

我在尝试从MongoDB中删除子文档时遇到了一个问题。

这是我的数据结构:

"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"
  }
]
} 

我尝试使用lodash删除一个参与者,但是lodash删除了所有参与者。你能帮我解决这个问题吗?

我更新了服务器端存在问题的整个代码

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));

在客户端,我使用相同的代码,并且可以使用

$http.delete(_deleteParApi)
        .success(msg => {
            $scope.parRemoved = msg;
            _.remove($scope.participants,{_id: pid})
        })  
        .catch(err => {
            console.log(err);
        })

3 个答案:

答案 0 :(得分:0)

这样做

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

DUPLICATE QUESTION

OR

Mongoose Models继承自Documents,它具有toObject()方法。我相信你要找的东西应该是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)
}

答案 1 :(得分:0)

根据lodash document,它会从数组中删除匹配的项目并返回已删除元素的数组,因此您无需将其分配回event.participants,但如果您这样做,则需要检查'not equal'以获取所需的返回数组。以下是修订后的代码:

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

如果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' 
}]

答案 2 :(得分:0)

使用mongo $ pull:

 Event.update(
    {"alias":alias},
    { $pull: { 'participants': { _id: pid } } }
);