2016-01-03 72 views
-1

您好我想從mongoDB中刪除一個值,但不是刪除特定的值,而是從架構中刪除所有用戶lol。從mongoDB數組中刪除值

var mongoose = require('mongoose'); 
var User = require('../../models/UserModel'); 

module.exports.unfollow = function(req, res){ 

    var thefollowee = req.body.followee; 
    var thefollower = req.body.follower; 


    User.find({_id: thefollower}).remove({following: thefollowee}).exec(); 

    User.find({_id: thefollowee}).remove({followers: thefollower}).exec(); 

    res.json({ message: 'Unfollowed'}); 




}; 

被關注被指向人的ID被人跟蹤, 跟隨指向誰跟隨被關注用戶的id。

+1

我剛纔看到你的答案彈出,但你也可能使用貓鼬的[findOneAndRemove](http://mongoosejs.com/docs/api .html#query_Query-findOneAndRemove)方法。 –

回答

0

行,所以我得到它通過使用$拉法

var mongoose = require('mongoose'); 
var User = require('../../models/UserModel'); 

module.exports.unfollow = function(req, res){ 

    var thefollowee = req.body.followee; 
    var thefollower = req.body.follower; 


     User.findByIdAndUpdate(thefollowee, { $pull: { followers: req.body.follower }}, function (err, user) { 

      if (err) 

      return handleError(err); 

     }); 

     User.findByIdAndUpdate(thefollower, { $pull: { following: req.body.followee }}, function (err, user) { 

      if (err) 

      return handleError(err); 

     }); 

    res.json({ message: 'Unfollowed'}); 




};