Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

javascript - MongoDB mongoose Deprecation Warning

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update:

5.2.10 is released and available for download here.

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 with projection 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.

npm install [email protected]

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.

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
DeprecationWarning: collection.save is deprecated. Use insertOne, insertMany, updateOne, or updateMany instead.
DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

All findOne* mongoose write methods by default use the findAndModify method which is deprecated in mongodb native driver.

Use mongoose.set('useFindAndModify', false); to have mongooose call the appropriate findOne* method on the mongodb native driver.

For remove and update replace those calls with delete* and update* methods respectively.

For save replace those calls with insert*/ update* methods respectively.

Use mongoose.set('useCreateIndex', true); to have mongooose call the createIndex method on the mongodb native driver.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...