sarunast
sarunast

Reputation: 2443

How to initialise mongoose schema in mongoDb?

I would like to to create database and collections for all schema/model files on node.js application startup/mongoose connection to db. (not to seed the database but initialise/set the structure schema on db)

My model:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

// define the schema
var modelSchema = new Schema({
    appId: {
        type: String,
        indexed: true
    }
});

// export the Model
module.exports = mongoose.model('Model', modelSchema)

And i am using this method to connect to DB:

mongoose.connect(mongodbUri, mongooseOptions);

At the moment mongoose creates the schema in db only when I insert something to the database.

Upvotes: 0

Views: 1648

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

If you fix indexed to be index in your schema, that will end up creating your collection so that its index can also be created when you create the model.

Upvotes: 2

Related Questions