Home > other >  collections (table names in other dbs) in mongodb
collections (table names in other dbs) in mongodb

Time:10-05

I created an express server for connection to my mongodb. Before I start, everything works fine. But I dont understand some things.

I made a collection in my mongodb, projects (with an 's' at the end)

In my code I made a project.model.js like: const Project = mongoose.model("Project", ...) module.exports = Project

Elsewhere I have db.project = require("./project.model");

Everything works like a harm, creating, updating, deleting. But I dont understand. My collections in my mongodb don't have the same name. The names are Users, Materials and in my code I use user, material. Maybe it is a stupid question but how does this work?

CodePudding user response:

It is the default behavior of mongoose to convert singular to plural model names.

To fix your issue & to convert your model name from plural to singular or any other name, You can write your code as shown below

 const Project = mongoose.model("Project", ProjectSchema, 'Project') 
 module.exports = Project

Now you could see the collection name as Project. The third parameter in the mongoose.model is the collection name.

CodePudding user response:

It is basically a behaviour of mongoose. It changes singular names to plural. You donot need to worry.

  • Related