nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
528 stars 118 forks source link

how to connect to mongodb replica set #653

Closed shine977 closed 3 years ago

shine977 commented 3 years ago

I'm trying to conenct mongo replicat set,but until retrying ....

Input Code


// app.module.ts
import { Module, } from '@nestjs/common';
import * as path from 'path';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ComponentModule } from './modules/component/component.module';
import { PageModule } from './modules/page/page.module';
import { FieldModule } from './modules/field/field.module';
import { V2Module } from './modules/v2/v2.module';
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule, ConfigService } from 'nestjs-config';
@Module({
    controllers: [AppController,],
    providers: [AppService,],
    imports: [ComponentModule, PageModule, FieldModule, V2Module,
        ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
        MongooseModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => {
                return configService.get('database')
            },
            inject: [ConfigService]
        })
    ]
})
export class AppModule { }

configService.get('database') executed result


{
  auto_reconnect: true,
  useNewUrlParser: true,
  useFindAndModify: false,
  uri: 'mongodb://1xx.x.x.1:27017,1x.x.x.1:27017,1x.x.x.1:27017/database',
  user: 'user',
  pass: 'pass',
  replset: 'set'
}

Mongoose example

https://mongoosejs.com/docs/connections.html#replicaset_connections


mongoose.connect('mongodb://user:pw@host1.com:27017,host2.com:27017,host3.com:27017/testdb');

Minimal reproduction of the problem with instructions

image

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

I can connect each database separately, but according to the mongoose example above, the connection keeps failing. What should I do? tanks!

Environment


Nest version: latest


For Tooling issues:
- Node version: latest 
- Platform:  Macos 10.15.7  

Others:

kamilmysliwiec commented 3 years ago

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