在Express和MongoDB中,我想通过ID删除文档。findOneAndDelete()不能通过_id删除,只能通过字段进行删除!为什么?
db.collection('quotes').findOneAndDelete({name: req.body.name}, (err, result) => {
if (err) return res.send(500, err)
})
var ObjectId = require('mongodb').ObjectId;
var collection = db.collection('quotes');
collection.remove({_id: new ObjectId(req.body.id)}, function(err, result) {
if (err) {
console.log(err);
} else {
res.send('A darth vadar quote got deleted')
}
});
var mongodb = require('mongodb');
db.collection('quotes', function(err, collection) {
collection.deleteOne({_id: new mongodb.ObjectID(req.body.id)});
});
三个函数的不同之处?
deleteOne
(用于单个文档)或deleteMany
(用于多个文档) - robertklep