I think that Mongoose tries to create a collection that already exists in MongoDB. This may happen if Mongoose is set to auto-create collections when schemas are defined or when the connection initializes.
Check your schema definitions and connection setup. You can prevent automatic collection creation by using autoCreate: false
in the schema options or setting autoIndex: false
globally if indexes are also being created.
const schema = new mongoose.Schema({
/* your schema definition */
}, { autoCreate: false });
Instead of attempting to create a collection every time, have a conditional check in place. Mongoose does not natively support this, but you can perform a MongoDB query using db.listCollections({ name: 'collection_name' })
to check if a collection exists before creating it.