Reputation: 2179
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
Upvotes: 176
Views: 69220
Reputation: 3
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()
!
Upvotes: -2
Reputation: 10790
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' })
Upvotes: 281
Reputation: 8586
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
Upvotes: 1
Reputation: 2695
//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.
Upvotes: 10
Reputation: 2928
Starting from mongoose 5.x you can disable it completely:
mongoose.pluralize(null);
Upvotes: 59
Reputation: 2954
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;
Upvotes: 2
Reputation: 613
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');
Upvotes: 12
Reputation: 949
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);
Upvotes: 64