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"
        })
    }

其他回答

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

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

or

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

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

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

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

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

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

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

概括来说,你可以使用:

SomeModel.find( $where, function(err,docs){
  if (err) return console.log(err);
  if (!docs || !Array.isArray(docs) || docs.length === 0) 
    return console.log('no docs found');
  docs.forEach( function (doc) {
    doc.remove();
  });
});

实现这一目标的另一种方法是:

SomeModel.collection.remove( function (err) {
  if (err) throw err;
  // collection is now empty but not deleted
});
db.collection.remove(<query>,
 {
  justOne: <boolean>,
  writeConcern: <document>
})