2017-02-24 40 views
0

我有一個文件「集合」的對象:貓鼬發現,並從數組中刪除一個文件

{ 
name: {type: String, required: true, default: "Untitled Collection"}, 
movies: [ 
     { the_id: {type: String}, 
      movie_id: {type: String}, 
      tmdb_id: {type: String}, 
      original_title: {type: String}, 
       release_date: {type:Date} 
     }], 
author: {type: String},} 

我需要找到和動畫[]刪除特定項目。這是我目前所擁有的,並且不起作用。

req.body是通過POST請求的數據傳遞的對象,並且具有所有必要的,以便能夠匹配一個在電影[]數組

Collection.findOne({_id : req.params.id}, (err, collection) =>{ 
     if(err){res.status(400).json(err);} 

     if(!collection) 
     { 
      res.status(404).json({message: 'No collection found'}); 
     }else{ 
      collection.movies.pop(req.body); 
      collection.save(); 

      res.json(collection); 
     } 
    }); 

所有它目前所做的是彈出關閉信息陣列中的前方對象,但我需要它來除去,其等於陣列中的對象到req.body

回答

1

調查$拉運營商

collection.movies.pull(req.body); 
0

可以在陣列中與刪除項目$pull

Collection.update({ 
    _id: req.params.id 
}, { 
    $pull: { 
     'movies': req.body 
    } 
}, (err, collection) => { 
    if (err) { 
     res.status(400).json(err); 
    } 
    console.log(res); 
    res.status(200); 
});