I'm trying to workout some of the CRUD operations for a MongoDB/NodeJS/Express application I'm working on, and I'm having difficult with the deleteOne command, presumably because it's not recognizing my query, although I don't know why.
I lifted the mongoDB code from the NodeJS driver documentation, and my code block looks like this:
DELETE REQUEST:
router.delete('/formula-list/:id', function(req, res){
var db = req.db.collection('formulas');
var id = req.params.id;
var query = { "_id": id };
db.deleteOne(query, function(err, r){
assert.equal(null, err);
assert.equal(1, r.deletedCount);
db.close();
});
res.end();
});
When I run this it returns the error AssertionError: 1 == 0, which I take to mean that the program doesn't have a document to delete.
However, if I do a console.log(req.params.id) I get 587f6ff4824d0a085c2b57bf, which is an _id for a document in my database:
console.log(db) returns the following:
console.log(db):
{ s:
{ pkFactory:
{ [Function: ObjectID]
index: 858167,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString],
isValid: [Function: isValid],
ObjectID: [Circular],
ObjectId: [Circular] },
db:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter] },
topology:
EventEmitter {
domain: null,
_events: [Object],
_eventsCount: 7,
_maxListeners: undefined,
clientInfo: [Object],
s: [Object] },
dbName: 'formulas',
options: { promiseLibrary: [Function: Promise], readConcern: undefined },
namespace: 'formulas.formulas',
readPreference:
{ _type: 'ReadPreference',
mode: 'primary',
tags: undefined,
options: undefined },
slaveOk: true,
serializeFunctions: undefined,
raw: undefined,
promoteLongs: undefined,
promoteValues: undefined,
promoteBuffers: undefined,
internalHint: null,
collectionHint: null,
name: 'formulas',
promiseLibrary: [Function: Promise],
readConcern: undefined } }
I take this to mean that the Mongo collection itself is also being recognized since it's not returning undefined.
Any advice on what I'm getting wrong would be greatly appreciated.

req.params.idto an ObjectId type first i.e.var ObjectID = require('mongodb').ObjectID; var id = new ObjectID(req.params.id);