Why does mongoose always add an s to the end of my collection name

node.jsMongodbMongoose

node.js Problem Overview


For example, this code results in a collection called "datas" being created

var Dataset = mongoose.model('data', dataSchema);

And this code results in a collection called "users" being created

var User = mongoose.model('user', dataSchema);

Thanks

node.js Solutions


Solution 1 - node.js

Mongoose is trying to be smart by making your collection name plural. You can however force it to be whatever you want:

var dataSchema = new Schema({..}, { collection: 'data' })

Solution 2 - node.js

API structure of mongoose.model is this:

Mongoose#model(name, [schema], [collection], [skipInit])

What mongoose do is that, When no collection argument is passed, Mongoose produces a collection name by pluralizing the model name. If you don't like this behavior, either pass a collection name or set your schemas collection name option.

Example:

var schema = new Schema({ name: String }, { collection: 'actor' });

or

schema.set('collection', 'actor');

or

var collectionName = 'actor'
var M = mongoose.model('Actor', schema, collectionName);

Solution 3 - node.js

Starting from mongoose 5.x you can disable it completely:

mongoose.pluralize(null);

Solution 4 - node.js

You can simply add a string as a third argument to define the actual name for the collection. Extending your examples, to keep names as data and user respectively:

var Dataset = mongoose.model('data', dataSchema, 'data');

var User = mongoose.model('user', dataSchema, 'user');

Solution 5 - node.js

//Mongoose's definition file. NOT your model files
1 const mongoose = require("mongoose");
2 mongoose.pluralize(null);

Adding the linemongoose.pluralize(null) in your Mongoose file will prevent collection name pluralization. You don't need to add this line to your model files.

As seen here.

Solution 6 - node.js

You can add the collection name as third parameter. See the example using Typescript:

import DataAccess = require('../DataAccess');
import IUser = require("../../Models/Interfaces/IUser");

var mongoose = DataAccess.mongooseInstance;
var mongooseConnection = DataAccess.mongooseConnection;

class UserSchema {
        static get schema () {
        var schema =  mongoose.Schema({
            _id : {
                type: String
            },
            Name: {
                type: String,
                required: true
            },
            Age: {
                type: Number,
                required: true
            }
        });

        return schema;
    }
}
var schema:any = mongooseConnection.model<IUser>("User", 
UserSchema.schema,"User");
export = schema;

Solution 7 - node.js

At the end of defining your schema on the next line Use this code

module.exports = mongoose.model("State", "StateSchema", "state")

Assuming that your state is what you want to use on your db to avoid s as states

Click the link to view the picture properly

Solution 8 - node.js

Mongoose compiles a model for you when you run this command

var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var child = mongoose.model('child', schema);

The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model child is for the children collection in the database.

Note: The .model() function makes a copy of schema. Make sure that you've added everything you want to schema, including hooks, before calling .model()!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBob RenView Question on Stackoverflow
Solution 1 - node.jsaaronheckmannView Answer on Stackoverflow
Solution 2 - node.jsNishank SinglaView Answer on Stackoverflow
Solution 3 - node.jsAndrey HohutkinView Answer on Stackoverflow
Solution 4 - node.jsAndreaView Answer on Stackoverflow
Solution 5 - node.js10110View Answer on Stackoverflow
Solution 6 - node.jssebuView Answer on Stackoverflow
Solution 7 - node.jsChukwuEmekaView Answer on Stackoverflow
Solution 8 - node.jsKushagraView Answer on Stackoverflow