FBFriendModel.find({
id: 333
}, function (err, docs) {
docs.remove(); //Remove all the documents that match!
});
上面的方法似乎不管用。记录还在那里。
有人能修好吗?
FBFriendModel.find({
id: 333
}, function (err, docs) {
docs.remove(); //Remove all the documents that match!
});
上面的方法似乎不管用。记录还在那里。
有人能修好吗?
当前回答
model.remove({title:'danish'}, function(err){
if(err) throw err;
});
裁判:http://mongoosejs.com/docs/api.html # model_Model.remove
其他回答
使用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();
Docs是一个文档数组。所以它没有一个mongoosemmodel .remove()方法。
可以分别迭代和删除数组中的每个文档。
或者—因为看起来您是通过(可能)唯一的id查找文档—使用findOne而不是find。
Remove()已弃用。使用deleteOne(), deleteMany()或bulkWrite()。
我使用的代码
TeleBot.deleteMany({chatID: chatID}, function (err, _) {
if (err) {
return console.log(err);
}
});
根据Samyak Jain的回答,我使用异步等待
let isDelete = await MODEL_NAME.deleteMany({_id:'YOUR_ID', name:'YOUR_NAME'});