简体   繁体   English

如何在mongoDb中初始化mongoose模式?

[英]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. 我想在node.js应用程序启动/与数据库的猫鼬连接上为所有架构/模型文件创建数据库和集合。 (not to seed the database but initialise/set the structure schema on db) (不播种数据库,而是在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. 目前,仅当我向数据库中插入内容时,猫鼬才会在db中创建模式。

如果您修复indexedindex架构中的,这最终将建立你的收藏,这样当你创建模型的指数也可以被创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM