Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare collection name and model name in mongoose

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....

like image 437
MMR Avatar asked Oct 17 '16 05:10

MMR


People also ask

Does Mongoose model create a collection?

The mongoose. model() function of the mongoose module is used to create a collection of a particular database of MongoDB.

How does Mongoose know which collection?

Mongoose uses the model name, as passed when it was created: mongoose. model("User", UserSchema) , converted to lower case and with an 's' appended. For the model User it uses the collection users by default. You can change this by explicitly specifying the collection name in the schema.

What is Mongoose model ()?

A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

What is __ V in MongoDB?

The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ).


2 Answers

Try-

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

As mentioned in docs: http://mongoosejs.com/docs/api.html#index_Mongoose-model

Mongoose#model(name, [schema], [collection], [skipInit])

Defines a model or retrieves it.

Parameters:

  • 1st param - name <String> model name
  • 2nd param - [schema] <Schema> schema name
  • 3rd param - [collection] <String> collection name (optional, induced from model name)
  • 4th param - [skipInit] <Boolean> whether to skip initialization (defaults to false)

See - https://stackoverflow.com/a/14454102/3896066

like image 57
saurav Avatar answered Sep 30 '22 15:09

saurav


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');
like image 28
Christopher Nolan Avatar answered Sep 30 '22 14:09

Christopher Nolan