What files require mongoDB connections

In one of my route files I have a mongoose connection:

const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI);
let db = mongoose.connection;

I also have the first 2 lines of the above in my model files.

I believe I need the library in order to access the Schema, plugin, and model methods. But I think the line that opens a connection to mongodb can be deleted from the model file. Is that correct?

Thanks

Yes, you only need to connect to your database once. No need for it in the model files.

1 Like