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!
});
上面的方法似乎不管用。记录还在那里。
有人能修好吗?
当前回答
如果只寻找一个要删除的对象,则可以使用
Person.findOne({_id: req.params.id}, function (error, person){
console.log("This object will get deleted " + person);
person.remove();
});
在本例中,Mongoose将根据匹配的req.params.id进行删除。
其他回答
Docs是一个文档数组。所以它没有一个mongoosemmodel .remove()方法。
可以分别迭代和删除数组中的每个文档。
或者—因为看起来您是通过(可能)唯一的id查找文档—使用findOne而不是find。
你可以直接在remove函数中使用查询,这样:
FBFriendModel.remove({ id: 333}, function(err){});
你可以随时使用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;
});
这招对我很管用,试试这个:
const id = req.params.id;
YourSchema
.remove({_id: id})
.exec()
.then(result => {
res.status(200).json({
message: 'deleted',
request: {
type: 'POST',
url: 'http://localhost:3000/yourroutes/'
}
})
})
.catch(err => {
res.status(500).json({
error: err
})
});
db.collection.remove(<query>,
{
justOne: <boolean>,
writeConcern: <document>
})