Mongoose: MongooseError: Cannot overwrite ''''' model once compiled, when attempting to connect to different databases that have the same model name

Created on 8 Jan 2019  路  4Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?
Bug or an issue

What is the current behavior?

When attempting to create 2 connection with 2 databases different database
database 1 : person_data mongodb://localhost:27017/person_data
database 2: car_data mongodb://localhost:27017/car_data

Now the catch is, both database have the same collection name (They both have a collection named information)

database 1 : person_data
collection : information

database 2 : car_data
collection: information

If the current behavior is a bug, please provide the steps to reproduce.

databaseConnection.ts

export const databaseConnection = (dbase: string) => {
  connect("mongodb://localhost:27017/" + dbase)  //Just pass the database you want to connect to
  )
};

And this is how my code looks like for retrieving person information: personData.ts

import { databaseConnection } from "../../../core/utils/mongodb";
import { model, Schema } from "mongoose";
import { ObjectID } from "bson";

databaseConnection("person_data"); //so you get mongodb://localhost:27017/person_data
let personInformationModel = model("information", new Schema({}));

export const personInformation = personInformationModel 
  .find()
  .exec();

for retrieving car information i created a new file carData.ts

import { databaseConnection } from "../../../core/utils/mongodb";
import { model, Schema } from "mongoose";
import { ObjectID } from "bson";

databaseConnection("car_data"); //so you get mongodb://localhost:27017/car_data
let carInformationModel = model("information", new Schema({}));

export const carInformation = carInformationModel 
  .find()
  .exec();

Main.ts

import { carInformation } from '../controllers/carInformation ';
import { personInformation } from '../controllers/carData';

router.get('/getInformation', function (req: Request, res: Response, next: any) {
    // get car information
    carInformation.then(response => {
        res.send(response);
    })

       // get car person information
    personInformation .then(response => {
        res.send(response);
    })
});

What is the expected behavior?
I should get data from both databases not this error
'MongooseError: Cannot overwrite information model once compiled.'

Please mention your node.js, mongoose and MongoDB version.
5.4.2

help

All 4 comments

You are nowhere specifying that mongoose should use a defined connection for some special models, so mongoose has no way of telling/guessing that.

See those 2 related Stackoverflow-examples of how to manage multiple different database-connections:

  1. https://stackoverflow.com/a/19475270/9069800

  2. https://stackoverflow.com/a/38516153/9069800

1) We do not officially support import { model, Schema } from "mongoose"; or any sort of destructured imports. See this FAQ. Use import mongoose from 'mongoose' unless you really know what you're doing.

2) export const databaseConnection = (dbase: string) => {}) Even if you skip using destructured imports, you're calling mongoose.connect() twice, so the 2nd connect() call will overwrite the first. Use mongoose.createConnection() instead:

export const databaseConnection = database => mongoose.createConnection(`mongodb://localhost:27017/${database}`, { useNewUrlParser: true });

Then use:

const db = databaseConnection("person_data"); //so you get mongodb://localhost:27017/person_data
const personInformationModel = db.model("information", new Schema({}));

Thanks @vkarpov15 for the tips , mongoose.createConnection() solved the issue. I was wondering is it safe and efficient to now close a connection after each operation (Read/write), or does mongoose handle that for you?

@Myolisi you shouldn't close a connection after each read/write, that would be horrible for performance.

Was this page helpful?
0 / 5 - 0 ratings