mongoose connection with authentication

Created on 11 Jan 2017  路  1Comment  路  Source: Automattic/mongoose

How do i execute query with authentication enabled?

Yesterday our app's mongodb data was deleted by some scripts. So that we enabled auth in our mongodb. But now i can't able to query the data. How do i enable auth using mongoose?

Most helpful comment

I normally do it 1 of 2 ways:

1) Connection string:

mongoose.connect('mongodb://username:password@host:port(usually 27017)/db')

Where username and password are the respective username and password for that specific db, host would be the host where your db is hosted (so localhost or some domain/IP), port is the port mongo listens on, and db is the name of the db you want to connect to

2) Using options. From the docs:

var options = {
  db: { native_parser: true },
  server: { poolSize: 5 },
  replset: { rs_name: 'myReplicaSetName' },
  user: 'myUserName',
  pass: 'myPassword',
  promiseLibrary: global.Promise
}
mongoose.connect(uri, options);

>All comments

I normally do it 1 of 2 ways:

1) Connection string:

mongoose.connect('mongodb://username:password@host:port(usually 27017)/db')

Where username and password are the respective username and password for that specific db, host would be the host where your db is hosted (so localhost or some domain/IP), port is the port mongo listens on, and db is the name of the db you want to connect to

2) Using options. From the docs:

var options = {
  db: { native_parser: true },
  server: { poolSize: 5 },
  replset: { rs_name: 'myReplicaSetName' },
  user: 'myUserName',
  pass: 'myPassword',
  promiseLibrary: global.Promise
}
mongoose.connect(uri, options);
Was this page helpful?
0 / 5 - 0 ratings