2016-01-03 69 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'}); 




};