Why does mongoose always add an s to the end of my collection name
Solution 1:
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:
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:
Starting from mongoose 5.x you can disable it completely:
mongoose.pluralize(null);
Solution 4:
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');