Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb Collection name

I've created a database in MongoDB using mongoose. Although everything works fine, but when I check mongodb the name of the collection has extra 's' in its name. The collection name created is employees. What could be wrong, or is it just the naming convention of mongoose?

const mongoose = require('mongoose');

let employeeSchema = mongoose.Schema({
  name: String,
  email: String,
  department: String,
  doj: Date,
  address: String
});

const Employee = mongoose.model("employee", employeeSchema);

module.exports = Employee;

2 Answers

It doesn't just add an extra 's' but it makes the correct plural of the name.

For Example : Mouse will be converted to mice

You can disable it by:

mongoose.pluralize(null);

Reference Link: https://github.com/Automattic/mongoose/issues/5947

like image 150
Ankit Manchanda Avatar answered Mar 04 '26 21:03

Ankit Manchanda


So you have two options for controlling document names in mongoose.

If you just want to disable pluralization, you can do it with mongoose.pluralize(null) as in Ankit's answer.

And if you want to change your collection name whatever you want, you can do:

mongoose.model("employee", employeeSchema, { collection: 'myEmployee' } ) 
like image 38
SuleymanSah Avatar answered Mar 04 '26 22:03

SuleymanSah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!