Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Can I set default value in Mongoose schema?

As mentioned by user whoami, mongoose only sets defaults on insert. If you are using mongoose 4. x and up and MongoDB 2.4. 0 and up you can opt-in to setting default values on update too.

Does Mongoose automatically create collection?

exports = mongoose. model('User',{ id: String, username: String, password: String, email: String, firstName: String, lastName: String }); It will automatically creates new "users" collection.

What does it mean that Mongoose is an ODM?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.

Why we use $set in mongoose?

By default, if you don't include any update operators in doc, Mongoose will wrap doc in $set for you. This prevents you from accidentally overwriting the document.


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' })


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);

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

mongoose.pluralize(null);

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');

//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.


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;

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