3

So I am using TypeScript to build an express server using mongoose to interact with MongoDB Atlas. Recently I decided to use multiple databases inside my app and change up my schemas to accommodate this new "architecture". However doing so made a deprecation warning appear:

(node:2096) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. (Use node --trace-deprecation ... to show where the warning was created)

I have already pinpointed where the error is happening. It is happening when I map the schema to a mongoose model.

Implementation code where error does occur:

const db = mongoose.connection.useDb("Authentication");
const User = db.model<UserInterface>("User", UserSchema);

Implementation code where error does NOT occur:

const User = mongoose.model<UserInterface>("User", UserSchema);

Here is my connection logic to mongodb:

import mongoose from "mongoose";

async function connect() {
   try {
      const uri = <string>process.env.MONGO_URI;
      await mongoose.connect(uri, {
         useNewUrlParser: true,
         useUnifiedTopology: true,
         useCreateIndex: true,
      });
      console.log("MongoDB Connected...");
      return;
   } catch (err) {
      process.exit(1);
   }
}

export default connect;

As you can see I already have useCreateIndex: true so I don't know why the error is occuring. The error does go away when I add this to the schema but I don't think its a good solution:

{ timestamps: true, autoIndex: false }

So what am I doing wrong here? Thank You!

  • 1
    Does this answer your question? [(node:63208) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead](https://stackoverflow.com/questions/51960171/node63208-deprecationwarning-collection-ensureindex-is-deprecated-use-creat) – Matt U Aug 30 '21 at 20:04
  • 1
    @MattU I take that back haha. Seems that setting using mongoose.set(...) prior to doing mongoose.connect() was the solution. I found it in one of the comments of the post you linked. Thanks! – camelCaseIsGoodPractice Aug 30 '21 at 20:12
  • Does this answer your question? [MongoDB mongoose Deprecation Warning](https://stackoverflow.com/questions/51916630/mongodb-mongoose-deprecation-warning) – PM 77-1 Aug 30 '21 at 20:47

1 Answers1

1

Ok so looks like the solution was to use mongoose.set(...) before using mongoose.connect(...). So this made my deprecation warning disappear:

import mongoose from "mongoose";

async function connect() {
   try {
      const uri = <string>process.env.MONGO_URI;
      mongoose.set("useNewUrlParser", true);
      mongoose.set("useUnifiedTopology", true);
      mongoose.set("useCreateIndex", true);
      mongoose.set("useFindAndModify", false);
      await mongoose.connect(uri);
      console.log("MongoDB Connected...");
      return;
   } catch (err) {
      process.exit(1);
   }
}

export default connect;

I always saw others setting these options inside the connect function so I never knew about the set function.