nlr_p nlr_p - 1 month ago 7
Node.js Question

How to put collection name in mongoose model

mY model,

var CategorySchema = new Schema({
categoryname: {
type: String
},
topictitle: {
type: String
},
topicdescription: {
type: String
},
tag: {
type: String
},
topicid: {
type: Schema.ObjectId
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('topics', CategorySchema);


Here topics is the name of my model but where can i keep my collection name?Can anyone suggest me help.

Answer

In the below model definition

mongoose.model('topics', CategorySchema);

var topics = mongoose.model('topics', CategorySchema,'topics' );
var tags = mongoose.model('tags', TagSchema, 'topics');

'topics' (last parameter) is the collection name & CategorySchema is the structure of a particular document

Check Mongoose documentation