How to declare collection name and model name in mongoose

mongoose - dynamic collection name
mongoose create vs save
mongoose.model collection name
mongoose deletemany
mongoose collection
mongoose delete all
mongoose connect
mongoose js

I have 3 kind of records,

1)Categories,
2)Topics  and
3)Articles 

In my mongodb, i have only 1 colection named 'categories' in which i stroe the above 3 types of documents.

For these 3 modules,i wrote 3 models(one each) in such a way like below,

mongoose.model('categories', CategorySchema);
mongoose.model('categories', TopicSchema)
mongoose.model('categories', ArticlesSchema)

like....mongoose.model('collection_name', Schema_name)

but when i run my code ,it throws error that

Cannot overwrite `categories` model once compiled.

If i change the above models like this,

mongoose.model('category','categories', CategorySchema);
mongoose.model('topics','categories', TopicSchema)
mongoose.model('articles','categories', ArticlesSchema)

It is creating 2 collections named topics and articles which i dont want. This is my issue right now,can anyone suggest me help.....Thanks....

How to declare collection name and model name in mongoose , provides an interface to the database for creating, querying, updating, deleting records, etc. Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. Set this option if you need a different name for your collection. var dataSchema = new Schema({..}, { collection: 'data'}); option: id

"The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name" - directly from the Model docs -- https://mongoosejs.com/docs/models.html

Setting the collection name, is used to track the revisions of a document. By default, its value is zero. Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. Set this option if you need a different name for your collection. var dataSchema = new Schema({..}, { collection: 'data'}); option: emitIndexErrors

As you stated, When you write this,

mongoose.model('categories', CategorySchema);
mongoose.model('categories', TopicSchema);
mongoose.model('categories', ArticlesSchema);

The output becomes inappropriate so do this instead,

mongoose.model('category', CategorySchema);
mongoose.model('topic', TopicSchema);
mongoose.model('article', ArticlesSchema);

Bonus tip. Mongoose makes your collection name plural by default so change a little code if you want your collection name as you wrote.

mongoose.model('category', CategorySchema,'category');
mongoose.model('topic', TopicSchema,'topic');
mongoose.model('article', ArticlesSchema,'article');

Setting the collection name, Defining your schema; Creating a model; Ids; Instance methods; Statics; Query Helpers Each schema maps to a MongoDB collection and defines the shape of the define a schema var animalSchema = new Schema({ name: String, type:  The first argument is the singular name of the collection that will be created for your model (Mongoose will create the database collection for the above model SomeModel above), and the second argument is the schema you want to use in creating the model.

You can use Mongoose Discriminators (http://mongoosejs.com/docs/discriminators.html) for that.

This article has a good and easy example of how to do that: https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-mongoose#using-mongoose-discriminators-to-store-data-in-a-single-collection.

Introduction to Mongoose for MongoDB, Having a default collection name is great when you're creating an application from scratch, but Using either the schema declaration, or; The model command​  When you call mongoose.model () on a schema, Mongoose compiles a model for you. var schema = new mongoose.Schema ( { name: 'string', size: 'string' }); var Tank = mongoose.model ('Tank', schema); The first argument is the singular name of the collection your model is for.

Understanding the mongoose __v field, Store the user login name in a global variable on server.js? I want: const SpendingList = mongoose.model(`${currentUser}${Spending}`, If i understand correctly, you want to create a seperate collection for each user. var mongoose = require ('mongoose'); var UserSchema = new mongoose.Schema({ name: String}); module.exports = mongoose.model("User", UserSchema); Beside the title property, the PostSchema also defines the reference by ObjectId for the postedBy property of the PostSchema as well as the postedBy property of the comments inside the comments array

Top 4 Reasons to Use Mongoose with MongoDB, The first argument is the singular name of the collection that will be created for your model (Mongoose will create the database collection for the  In this case it is "mongoose_demo". Skipping //this value defaults the name of the collection to plural of model name i.e books. var Book = mongoose.model('Book', bookSchema, "mongoose_demo"); Lets now see how to insert, read, update and delete records using the Model created above.

Mongoose v5.9.16: Models, create a model by using mongoose.connect(), with the name of our collection, and the schema object we just created; use the model as a  A Model is an object that gives you easy access to a named collection, allowing you to query the collection and use the Schema to validate any documents you save to that collection. It is created by combining a Schema, a Connection, and a collection name.

Comments
  • you have to define 3 models for 3 schema
  • Said differently, a model maps directly to one document of a collection. What you're trying to do here isn't possible -- since really you have one model/collection in your DB that contains documents which themselves contain a type property you are using to differentiate on.
  • "The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name" - directly from the Model docs -- mongoosejs.com/docs/models.html