0

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:

MongoDB document

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.

1
  • You need to cast the req.params.id to an ObjectId type first i.e. var ObjectID = require('mongodb').ObjectID; var id = new ObjectID(req.params.id); Commented Feb 13, 2017 at 14:11

1 Answer 1

1

Native mongodb library needs _id to be ObjectId

End result would look more-less like this:

var mongo = require('mongodb');

// Other code lines

router.delete('/formula-list/:id', function(req, res){
  var db = req.db.collection('formulas');
  var query = { "_id": new mongo.ObjectId(req.params.id) };
  db.deleteOne(query, function(err, r){
    assert.equal(null, err);
    assert.equal(1, r.deletedCount);
    db.close();
  });
  res.end();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.