Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 577 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MongoDB mongoose Deprecation Warning

#1
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***
Reply

#2
You can do a `npm install [email protected]` and this will help you get back to an earlier version which will not show any deprecation warnings
Reply

#3
mongoose.connect('your db url', {
useCreateIndex: true,
useNewUrlParser: true
})

or

mongoose.set('useCreateIndex', true)
mongoose.connect('your db url', { useNewUrlParser: true })
Reply

#4
After upgrading to version 5.2.10. Any of the options below can be use


const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test', {

useCreateIndex: true,
useNewUrlParser: true

})
.then(() => console.log('connecting to database successful'))
.catch(err => console.error('could not connect to mongo DB', err));


<hr>or<hr>


const mongoose = require('mongoose');

mongoose.set('useCreateIndex', true);

mongoose.connect('mongodb://localhost/test',{

useNewUrlParser: true

})
.then(() => console.log('connecting to database successful') )
.catch(err => console.error('could not connect to mongo DB', err) );


Reply

#5

This worked for me at April, 2020:

mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
Reply

#6
`mongoose.connect('mongodb://localhost:27017/tablename',{ useUnifiedTopology: true, useNewUrlParser: true,useCreateIndex: true },()=>{
console.log(`connected db`)
});`
Reply

#7
**Update:**

5.2.10 is released and available for download [here](

[To see links please register here]

).

For more info on the docs you can view page

[To see links please register here]


For more info on the issue and its fix

[To see links please register here]


**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`](

[To see links please register here]

) 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`][1] 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.




[1]:

[To see links please register here]


Reply

#8
Just pass following options while data base connection

for e.g.

const mongoose = require("mongoose");

mongoose.connect("uri",{
"useNewUrlParser": true,
"useUnifiedTopology": true,
"useCreateIndex": true
// other deprecations
},(err)=>{
// connection logging
});

Reply

#9
I am currently using "[email protected]"
you can get rid of the DeprecationWarning by using this,

**Method 1**

mongoose.connect("Your DB address", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true // to handle collection.ensureIndex is deprecated
});

**Method 2**

mongoose.connect("Your DB address", {
useNewUrlParser: true,
useUnifiedTopology: true, // other deprecation warnings
});
mongoose.set("useCreateIndex", true); // to handle collection.ensureIndex is deprecated
Reply

#10
You can also use
--no-deprecation
in CLI to ignore the deprecation warnings.

I was getting this warning- (node:108) [MONGODB DRIVER] Warning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
by using --no-deprecation it worked fine

Check documentation here -

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through