nestjs / docs.nestjs.com

The official documentation https://docs.nestjs.com 📕
MIT License
1.2k stars 1.74k forks source link

Mongoose: auth against admin database but connect to another #878

Closed MarkusAnthony closed 4 years ago

MarkusAnthony commented 4 years ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[ X ] Documentation issue or request (new chapter/page)
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I want to authenticate against "admin" database but open another database.

Expected behavior

I would like to specify the database I want to "open" and query against.

Minimal reproduction of the problem with instructions

app.module.ts


import { ConfigService } from './config/config.service';
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { ConfigModule } from './config/config.module';
import { CaseAssetsModule } from './case-assets/case-assets.module';

@Module({
  imports: [
    ConfigModule,
    CaseAssetsModule,
    MongooseModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        uri: configService.getString('mongodb.uri'),
        useNewUrlParser: true,
        useUnifiedTopology: true,
      }),
      inject: [ConfigService],
    })
  ],
  controllers: [],
  providers: [],
})
export class AppModule { }

Connection string:

mongodb://<myuser>>:<<mypwd>>#@<<myserver>:27017/admin

What is the motivation / use case for changing the behavior?

Environment


Lastest version of nodejs, nestjs, mongoose

Others:

galesky commented 4 years ago

I think you may have created this issue at the wrong repo, but anyhow, the easiest way is to specify authSource in the uri

const uri = 'mongodb://monguser:mongpass@192.168.1.1:27017/DATABASE?authSource=admin';

If you need more info, see this issue https://github.com/Automattic/mongoose/issues/3905

kamilmysliwiec commented 4 years ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.