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

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

有人能修好吗?


当前回答

.remove()类似于.find():

MyModel.remove({search: criteria}, function() {
    // removed.
});

其他回答

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

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

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

如果你不喜欢迭代,那就试试

FBFriendModel.find({ id:333 }).remove( callback );

or

FBFriendModel.find({ id:333 }).remove().exec();

find返回一个Query,它有一个remove函数。

更新为Mongoose v5.5.3 - remove()现在已弃用。使用deleteOne(), deleteMany()或findOneAndDelete()代替。

我更喜欢承诺符号,在这里你需要。

Model.findOneAndRemove({_id:id})
    .then( doc => .... )

你可以直接在remove函数中使用查询,这样:

FBFriendModel.remove({ id: 333}, function(err){});
model.remove({title:'danish'}, function(err){
    if(err) throw err;
});

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