How to set useMongoClient (Mongoose 4.11.0)?
node.jsMongodbMongoosenode.js Problem Overview
This is the code I use to connect to my database:
private connectDatabase(databaseUri: string): Promise<Mongoose.Connection> {
return Mongoose.connect(databaseUri).then(() => {
debug('Connected to MongoDB at %O', databaseUri);
return Mongoose.connection;
});
}
Today I updated Mongoose to version 4.11.0 and I got this warning when running my tests:
(node:4138) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0,
use `openUri()` instead, or set the `useMongoClient` option if using `connect()`
or `createConnection()`
I can't find any information on how to "set useMongoClient".
Do you guys know how to?
node.js Solutions
Solution 1 - node.js
This is how you use useMongoClient:
mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true })
Solution 2 - node.js
Add { useMongoClient: true } as another argument to connect or createConnection method, its depends on version of mongoose you are using.
// Using `mongoose.connect`...
var promise = mongoose.connect('mongodb://localhost/myapp', {
useMongoClient: true,
/* other options */
});
// Or `createConnection`
var promise = mongoose.createConnection('mongodb://localhost/myapp', {
useMongoClient: true,
/* other options */
});
Solution 3 - node.js
mongoose.connection.openUri('mongodb://127.0.0.1/camp_v12')
has anyone tried this? my deprecated warning disappeared when i use this, it was from the documentation
Solution 4 - node.js
The easiest fix for this would be to open your terminal, change your directory to your root project (folder where package.json
is)
Run:
npm remove mongoose
then:
npm install [email protected] --save
problem solved.
Upgrading is not always the best option.
Solution 5 - node.js
Without Typescript you can pretty much ignore the issue and use Mongoose.connect(databaseUri, { useMongoClient: true })
.
If you really want to avoid having the warning avoid the version 4.11.0.
I updated to version 4.11.1 and since @types/[email protected] are not yet updated and they do not mention the field useMongoClient
in the ConnectionOptions
, this is how i imported the module:
const Mongoose = require('mongoose');
And then used this function:
private connectDatabase(databaseUri: string): Promise<any> {
return Mongoose.connect(databaseUri, { useMongoClient: true })
.then(() => {
console.log('Connected to MongoDB at ', databaseUri);
return Mongoose.connection;
})
.catch(err => debug(`Database connection error: ${err.message}`));
}
Solution 6 - node.js
As many answers say, adding { useMongoClient: true }
in options argument to connect
or createConnection
method will solve the problem.
For e.g.
// With mongoose.connect method
mongoose.connect('mongodb://localhost/app', { useMongoClient: true });
// With createConnection method
mongoose.createConnection('mongodb://localhost/app', { useMongoClient: true });
But what is MongoClient in the first place?
From MongoDB Node.js Driver version 1.2, a new connection class MongoClient was introduced that has the same name across all the official drivers.
The new connection class MongoClient acknowledges all writes to MongoDB, in contrast to the existing DB connection class which has acknowledgements turned off.
So { useMongoClient: true }
tells moongoose to use the new connection class rather than the old one
For more info click here
Solution 7 - node.js
Connect to MongoDB with Mongoose 4.11.x (tested with both mLab single instance and MongoDB Atlas replica set):
const mongoose = require('mongoose');mongoose.Promise = global.Promise;
const options = { promiseLibrary: global.Promise, useMongoClient: true, };
function connect() { mongoose.connect(URI, options) .then(function() { const admin = new mongoose.mongo.Admin(mongoose.connection.db); admin.buildInfo(function(err, info) { if (err) { console.err(
Error getting MongoDB info: ${err}
); } else { console.log(Connection to MongoDB (version ${info.version}) opened successfully!
); } }); }) .catch((err) => console.error(Error connecting to MongoDB: ${err}
)); }module.exports = connect;
Create model:
const mongoose = require('mongoose');const userSchema = new mongoose.Schema({...});
module.exports = mongoose.model('User', userSchema);
Solution 8 - node.js
According to the mongoose
documentation, this is how useMongoClient
can be set.
function connectDatabase(databaseUri){
var promise = mongoose.connect('mongodb://localhost/myapp', {
useMongoClient: true,
});
return promise;
}
Solution 9 - node.js
The easiest fix for this:
Run this command in project root folder via terminal:
npm remove mongoose
npm install [email protected] --save
Problem solved.
Upgrading is not always the best option.
Solution 10 - node.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/starbucks', {
useMongoClient: true
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('openUri', function() {
// we're connected!
});
Solution 11 - node.js
Error:
(node:1652) DeprecationWarning: open()
is deprecated in mongoose >= 4.11.0, use openUri()
instead, or set the useMongoClient
option if using connect()
or createConnection()
.
Solution: To set useMongoClient:true in connection property
var promise = mongoose.connect('mongodb://localhost/myapp', {
useMongoClient: true,
/* other options */
});
See http://mongoosejs.com/docs/connections.html#use-mongo-client now listening for request
Solution 12 - node.js
It works for me using Typescript:
var dbOpt : any = {
useMongoClient: true
}
mongoose.connect(dbURI, dbOpt);
Solution 13 - node.js
Upgrade mongoose , first you need to uninstall it with this code:
npm uninstall mongoose
Then install it again with the following code :
npm install mongoose --save