Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 881
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 8
Kenil Vasani
Asked: December 19, 20202020-12-19T21:58:53+00:00 2020-12-19T21:58:53+00:00

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

  • 8

I am using mongoose findOneAndUpdate but still getting the error,

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

But I am not even using findAndModify, why is it converting my query to findAndModify?

deprecatedmongodbmongoosenode.js
  • 1 1 Answer
  • 12 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-19T21:55:51+00:00Added an answer on December 19, 2020 at 9:55 pm

      You need to set the option in the query useFindAndModify to false, as mentioned in the docs.

      (search keyword Currently supported options are)

      ‘useFindAndModify’: true by default. Set to false to make
      findOneAndUpdate() and findOneAndRemove() use native
      findOneAndUpdate() rather than findAndModify().

      and if you see the definition file of mongoose, where mentioned that it calls findAndModify update command.

       /**
        * Issues a mongodb findAndModify update command.
        * Finds a matching document, updates it according to the update arg, 
          passing any options,
        * and returns the found document (if any) to the callback. The query 
          executes immediately
        * if callback is passed else a Query object is returned.
        */
       findOneAndUpdate(): DocumentQuery<T | null, T>;
      

      Recently updated in the mongoose docs (Click here) for these deprecation where mentioned:

      Mongoose’s findOneAndUpdate() long pre-dates the MongoDB driver’s
      findOneAndUpdate() function, so it uses the MongoDB driver’s
      findAndModify() function instead.

      There are three ways or more by which you can avoid the use of FindAndModify:

      1. At Global level: Set the option to false.
      // Make Mongoose use `findOneAndUpdate()`. Note that this option is `true`
      // by default, you need to set it to false.
      mongoose.set('useFindAndModify', false);
      
      1. At connection level: we can configure using the connection options:
          mongoose.connect(uri, { useFindAndModify: false });
      
      1. At Query level:
         await ModelName.findOneAndUpdate({matchQuery},
         {$set: updateData}, {useFindAndModify: false});
      
      
      • 6
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      SQLSTATE[HY000]: General error: 1835 Malformed communication packet on LARAVEL

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com