While querying the documents by using collection.find
I started getting following warning in my console
DeprecationWarning: collection.find option [fields] is deprecated and will be removed in a later version
Why am I seeing this and how do I fix this? (Possible alternatives)
EDIT: Query Added
Session
.find({ sessionCode: '18JANMON', completed: false })
.limit(10)
.sort({time: 1})
.select({time: 1, sessionCode: 1});
Mongoose version 5.2.9
Update:
5.2.10 is released and available for download here.
Use
mongoose.set('useCreateIndex', true);
to have mongooose call thecreateIndex
method on the mongodb native driver.For more info on the docs you can view page https://mongoosejs.com/docs/deprecations
For more info on the issue and its fix https://github.com/Automattic/mongoose/issues/6880
Original Answer:
Mongoose 5.2.9 version upgraded the native mongodb driver to 3.1.3 in which changes were added to throw warning messages when the deprecated native driver method is called.
fields
option is deprecated and is replaced withprojection
option.You will have to wait for mongoose to make changes at their end to replace the fields option with projection. The fix is scheduled for 5.2.10 release.
For time being you can go back to 5.2.8 which will suppress all deprecation warnings.
For all other deprecated warnings you have to approach them case by case.
You will see other deprecation warnings when you use other collection methods.
All
findOne*
mongoose write methods by default use thefindAndModify
method which is deprecated in mongodb native driver.Use
mongoose.set('useFindAndModify', false);
to have mongooose call the appropriatefindOne*
method on the mongodb native driver.For
remove
andupdate
replace those calls withdelete*
andupdate*
methods respectively.For
save
replace those calls withinsert*
/update*
methods respectively.or
You can do a
npm install mongoose@5.2.8
and this will help you get back to an earlier version which will not show any deprecation warningsAfter upgrading to version 5.2.10. Any of the options below can be use