Jonathan Smith
Jonathan Smith

Reputation: 2599

Mongoose pluralization rules not working

Reading up on the mongoose it appears that if I declare a mongoose model like this:

var User = mongoose.model('user', userSchema)

Then mongoose will create a collection called "users" (with an 's').

However, I have already manually created a collection called "Regions" in mongo. When I try to declare my mongoose model, it looks like this:

var Region = mongoose.model('Region', regionSchema)

But then when I try to return all objects using Region.find(), zero results are returned. So I then tried:

var Region = mongoose.model('Regions', regionSchema)

And this also returned zero results.

In the end I had to do this:

var Region = mongoose.model('Region', regionSchema, 'Results')

If mongoose has pluralization rules, how come I still need to pass in the collection name in order for it to find the data?

Upvotes: 0

Views: 137

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312149

It's because Mongoose both pluralizes the model name and converts it to lower case. So with a model name of 'Region' it was looking in the regions collection.

So you'd need to provide an explicit collection name in the model call (like you show), but with a collection name of 'Regions':

var Region = mongoose.model('Region', regionSchema, 'Regions')

Upvotes: 1

Related Questions