1

I'm confused. I tried to use the findAndModify function of a collection in NodeJS as it was stated in the official MongoDB documentation:

db.collection('myColl').findAndModify(
    {
        query: {
            itemId: ObjectID(itemId),
            requesterId: requesterId
        },
        remove: 1
    },
    function(err, found){ 
        console.log(err, found);
    });

It works fine in the console. But in NodeJS I retrieve an error. The error message told me, I have to provide at least one update or remove. There were some similar questions here on StackOverflow where one of the answers mentioned to use findAndRemove instead.

Okay, but as I searched for a documentation for findAndRemove I haven't found anything helpful. Also, my IDE (WebStorm) marks that method as deprecated. Through the autocomplete, I've also found the function findOneAndDelete which isn't marked as deprecated and seems to do what I want, but I cannot find anything about that function.

Since, I am quite new to NodeJS as well as MongoDB, this stuff is really confusing and I'm very disappointed of this documentation.

My question is: What is the background? Where can I find a useful documentation for the JavaScript API of MongoDB or how to use the MongoDB documentation correctly? Where can I get information on the above-mentioned method?

4
  • What mongodb module are you using? Can you provide the documentation you are reading? Commented Dec 10, 2015 at 11:48
  • mongodb.github.io/node-mongodb-native/2.1/api/… Commented Dec 10, 2015 at 11:50
  • have you included var ObjectID = require('mongodb').ObjectID ? Commented Dec 10, 2015 at 11:50
  • 1. I'm using the native drivers of MongoDB (require('mongodb')) 2. Yes, I've included ObjectID Commented Dec 10, 2015 at 12:06

3 Answers 3

3

I assume you are using the node MongoDB driver.

You can find the documentation of the findAndModify function, aswell as Inserting and updating functions here:

https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html

Sign up to request clarification or add additional context in comments.

Comments

3

There are many Node.js modules act to connect and manage a MongoDB server.

The native one is this: https://github.com/mongodb/node-mongodb-native

But you can use also the most popular one Mongoose: https://github.com/Automattic/mongoose

Said that I think your code is wrong it should be:

db.collection('myColl').findAndModify({
    itemId: ObjectID(itemId),
    requesterId: requesterId
}, null, null, {
    remove: 1
},
function(err, found) {
    console.log(err, found);
});

As stated in the doc: findAndModify(query, sort, doc, options, callback)

Comments

0
db.collection('myColl').findAndModify(
   { itemId: ObjectID(itemId), requesterId: requesterId },
   {}, //Do an update here if you want
   { remove: true }, // Remove the doc
   function (err, doc) {
      // do things 
   }
);

https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#example

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.