MongoDB deprecation warnings when using Mongoose

Viewed 2823

I'm using mongoose version 5.2.13, the latest I suppose. But when I try executing the .findOneAndUpdate() query, Mongo throws a deprecation warning:

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

Is this known behavior that should be fixed in future versions of mongoose? Or should I do something about it? The query I'm running is:

output2 = await dbUser.findOneAndUpdate(
   { _id: args.authorid },
   { $push: { posts: newpost2 } },
);

I'm not very comfortable downgrading my packages in case that's a possible suggestion as offered in MongoDB mongoose collection.find options Deprecation Warning.

2 Answers

Reference : https://mongoosejs.com/docs/deprecations.html

Add Param : useFindAndModify: false

Eg Code:

    mongoose.connect(
        MONGODB_URL, {
        useFindAndModify: false
      },
      (err, db) => {
        if (err) throw err;
        console.log(`MongoDB connected on port ${MONGODB_PORT}`);
      }
    );

I think to avoid this warning you have to add useFindAndModify: false to the connection options as mentioned in Mongoose documentation

as the following:

mongoose.connect(uri, { useFindAndModify: false });


Also, you can upgrade it to (v5.12.15); which is the latest version; besides using the earlier mentioned connection option

Related