Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose connect throwing a warning

I am writing a mongodb connected app and while connecting to the server like bellow getting a warning like this:

Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.

My codes are like bellow

import mongoose from 'mongoose';
import config from './config';
mongoose.connect(config.db.uri);

at config.js

const config = {
    name: 'API',
    version: '0.0.1',
    env: process.env.NODE_ENV || 'development',
    port: process.env.PORT || 3000,
    base_url: process.env.BASE_URL || 'http://localhost:3000',
    db: {
        uri: 'mongodb://admin:[email protected]:27017/ai?authSource=admin',
    },
}

export default config;

I am using Node v8.0.0 and mongoose 4.10.5

like image 800
Dave Avatar asked Jun 07 '17 21:06

Dave


People also ask

What can I use instead of useCreateIndex?

Set the useCreateIndex global option to opt in to making Mongoose use createIndex() instead.

Is useNewUrlParser deprecated?

Is useNewUrlParser deprecated? The useNewUrlParser Option DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version.

What is deprecation warning in MongoDB?

The MongoDB server has deprecated the count() function in favor of two separate functions, countDocuments() and estimatedDocumentCount() . DeprecationWarning: collection. count is deprecated, and will be removed in a future version.

What is useUnifiedTopology true?

useUnifiedTopology: False by default. Set to true to opt in to using the MongoDB driver's new connection management engine. You should set this option to true , except for the unlikely case that it prevents you from maintaining a stable connection.


1 Answers

The warning is due to the MongoDB driver deprecating the API used by mongoose's default connection logic. As of mongoose 4.11.1 you can opt to use mongo client by setting the useMongoClient option, for example

mongoose.connect(config.db.uri, { useMongoClient: true, /* other options */ })

Be reminded that using Mongo native client may have unwanted effects, so be sure to test everything out exhaustively
See more details here

like image 145
3 revs Avatar answered Oct 04 '22 21:10

3 revs