可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
This is how you use useMongoClient:
mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true })
回答2:
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 */
});
回答3:
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
http://mongoosejs.com/docs/connections.html
回答4:
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 mongoose@4.10.8 --save
problem solved.
Upgrading is not always the best option.
http://mongoosejs.com/docs/connections.html
回答5:
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/mongoose@4.7.18 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}`));
}
回答6:
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);
回答7:
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 MongoClinet 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 connection class Db that 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
回答8:
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;
}
回答9:
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!
});
回答10:
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
回答11:
The easiest fix for this:
Run this command in project root folder via terminal:
npm remove mongoose
npm install mongoose@4.10.8 --save
Problem solved.
Upgrading is not always the best option.
https://github.com/Automattic/mongoose/issues/5399
回答12:
The easiest fix for this; "npm remove mongoose"
then "npm install mongoose@4.10.8 --save"
problem solved. Upgrading is not always the best option.
回答13:
It works for me using Typescript:
var dbOpt : any = {
useMongoClient: true
}
mongoose.connect(dbURI, dbOpt);