Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prevent MongoDB adding plural form to collection names?

Tags:

It all works fine until I want to create a collection named 'Mice' for example. Both Mouses and Mices are unacceptable. Would be good if there is an option to set this in the config. Comments: thanks for the suggestion, I am using Mongoose.

like image 448
swogger Avatar asked Mar 13 '14 21:03

swogger


2 Answers

If you name your model 'mouse', Mongoose will actually pluralize the collection name correctly to 'mice' (see source code).

But you can also explicitly name your collection when creating a model by passing it as the third parameter to model:

var Mice = mongoose.model('Mice', MouseSchema, 'Mice'); 
like image 146
JohnnyHK Avatar answered Oct 04 '22 20:10

JohnnyHK


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

For more information check this link: http://mongoosejs.com/docs/api.html#index_Mongoose-model

like image 26
Nishank Singla Avatar answered Oct 04 '22 18:10

Nishank Singla