FBFriendModel.find({
    id: 333
}, function (err, docs) {
    docs.remove(); //Remove all the documents that match!
});

上面的方法似乎不管用。记录还在那里。

有人能修好吗?


当前回答

使用remove()方法,您可以删除。

getLogout(data){
        return this.sessionModel
        .remove({session_id: data.sid})
        .exec()
        .then(data =>{
            return "signup successfully"
        })
    }

其他回答

对于删除文档,我更喜欢使用Model。(回调)remove(条件)

请参考API文件删除:-

http://mongoosejs.com/docs/api.html#model_Model.remove

对于这种情况,代码将是:-

FBFriendModel.remove({ id : 333 }, function(err, callback){
console.log(‘Do Stuff’);
})

如果你想在不等待MongoDB响应的情况下删除文档,不传递回调,那么你需要对返回的查询调用exec

var removeQuery = FBFriendModel.remove({id : 333 });
removeQuery.exec();

如果只寻找一个要删除的对象,则可以使用

Person.findOne({_id: req.params.id}, function (error, person){
        console.log("This object will get deleted " + person);
        person.remove();

    });

在本例中,Mongoose将根据匹配的req.params.id进行删除。

你可以随时使用Mongoose内置函数:

var id = req.params.friendId; //here you pass the id
    FBFriendModel
   .findByIdAndRemove(id)
   .exec()
   .then(function(doc) {
       return doc;
    }).catch(function(error) {
       throw error;
    });

这对我来说是3.8.1版本最好的:

MyModel.findOneAndRemove({field: 'newValue'}, function(err){...});

它只需要一个DB调用。 如果在搜索和删除之前不执行任何删除操作,则使用此方法。

db.collection.remove(<query>,
 {
  justOne: <boolean>,
  writeConcern: <document>
})