Search code examples
nestjsnestjs-mongoose

NestJS: Unable to connect to MongoDB using MongooseModule.forRootAsync


I am trying to connect to local mongoDB in the AppModule by doing the following but it won't connect:

imports: [
    MongooseModule.forRootAsync({
      useFactory: async () => {
        return {
          uri: 'mongodb://localhost:27017/client',
          useNewUrlParser: true,
          useCreateIndex: true,
        };
      },
    }),
  ],

Error from NestJS: [MongooseModule] Unable to connect to the database. Retrying (1)...

The MongoDB is running fine. I can connect to it through MongoDB Compass with the same uri.

What is done wrong causing the connection not being established?


Solution

  • useNewUrlParser , useUnifiedTopology , useFindAndModify , and useCreateIndex are no longer supported options,İf you remove that option everything will works.From the Mongoose 6.0 docs:

    MongooseModule.forRootAsync({
          useFactory: async () => ({
            uri: 'mongodb://localhost:27017/client',
          }),
        }),