MongoDB collection name

I followed the MongoDB and Mongoose section in the API and Microservices curriculum. In the lesson, we created a model Person, so I expected there’s a collection named Person in the MongoDB database. But when I looked the database on the MongoDB cloud, the collection name is ‘people’. This collection includes four ‘Person’ objects (documents) consisting of {_id, name, age, favoriteFoods}.

Why there’s no Person collection, but people collection?

Mongoose does this cool thing where it pluralises the name you give your collection. There are a few ways to turn this off, if you wish.

Aha, so mongoose is the one doing it. Thanks for replying.

In a way it make sense to name the collection of a model in a plural form, and do it automatically by default. But I prefer to specify the collection name explicitly myself when creating a model to make all the names known just by reading the statement.

I believe you can force the collection name to be whatever you’d like.

mongoose.model('Model name', schema, 'Collection name');

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.