【问题标题】:How to initialise mongoose schema in mongoDb?如何在 mongoDb 中初始化猫鼬模式?
【发布时间】:2015-02-14 00:02:37
【问题描述】:

我想为 node.js 应用程序启动/mongoose 连接到 db 上的所有模式/模型文件创建数据库和集合。 (不是为数据库播种,而是在 db 上初始化/设置结构模式)

我的模特:

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)

我正在使用这种方法连接到数据库:

mongoose.connect(mongodbUri, mongooseOptions);

目前 mongoose 仅​​在我向数据库中插入内容时才会在 db 中创建模式。

【问题讨论】:

  • 是的,你是对的,这是一个愚蠢的错误。我虽然那个索引选项确实存在。

标签: node.js mongodb mongoose


【解决方案1】:

如果您将架构中的 indexed 修复为 index,则最终会创建您的集合,以便在您创建模型时也可以创建其索引。

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 1970-01-01
    • 2019-02-23
    • 2019-10-02
    • 2014-09-25
    • 2021-09-22
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多