Search code examples
node.jsmongodbmongoosemean-stackmeanjs

Mongoose js create collection on start


I have a MEAN js app. I need the collections to get created when it is started. Right now, it only gets created when I insert a document in it.

Can something be specified during schema creation?


Solution

  • One way to do it is to make sure you have at least one field indexed. Mongoose will create the collection when it creates the indexes.

    For example, to index the name field, specify this in your schema: name: {type: String, index: true}.