猫鼬更改架构格式 [英] Mongoose Changing Schema Format

查看:46
本文介绍了猫鼬更改架构格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在快速开发使用Mongoose的应用程序,并且我们的架构经常在变化.我似乎无法找出适当的方法来更新现有文档的架构,而又不会遗忘它们并从头开始完全重新创建它们.

我遇到了 http://mongoosejs.com/docs/api.html#schema_Schema-add,看起来是正确的.几乎没有关于如何实际实现此方法的文档,这对于MongoDB的新手来说非常困难.

我只是想添加一个名为enabled的新字段.我的架构定义是:

var sweepstakesSchema = new Schema({
    client_id: {
        type: Schema.Types.ObjectId,
        ref: 'Client',
        index: true
    },
    name: {
        type: String,
        default: 'Sweepstakes',
    },
    design: {
        images: {
            type: [],
            default: []
        },
        elements: {
            type: [],
            default: []
        }
    },
    enabled: {
        type: Boolean,
        default: false
    },
    schedule: {
        start: {
            type: Date, 
            default: Date.now
        },
        end: {
            type: Date,
            default: Date.now
        }
    },
    submissions: {
        type: Number,
        default: 0
    }
});

解决方案

Mongoose并没有内置任何有关迁移现有文档以适应架构更改的内容.您需要根据需要在自己的代码中执行此操作.在诸如新的enabled字段的情况下,编写代码可能是最干净的方法,以便将丢失的enabled字段当作设置为false的字段一样对待,因此您不必接触现有文档. /p>

就架构本身而言,您只需按照显示的内容更新Schema定义,但是诸如具有default值的新字段之类的更改只会影响以后的新文档.

We're rapidly developing an application that's using Mongoose, and our schema's are changing often. I can't seem to figure out the proper way to update a schema for existing documents, without blowing them away and completely re-recreating them from scratch.

I came across http://mongoosejs.com/docs/api.html#schema_Schema-add, which looks to be right. There's little to no documentation on how to actually implement this, making it very hard for someone who is new to MongoDB.

I simply want to add a new field called enabled. My schema definition is:

var sweepstakesSchema = new Schema({
    client_id: {
        type: Schema.Types.ObjectId,
        ref: 'Client',
        index: true
    },
    name: {
        type: String,
        default: 'Sweepstakes',
    },
    design: {
        images: {
            type: [],
            default: []
        },
        elements: {
            type: [],
            default: []
        }
    },
    enabled: {
        type: Boolean,
        default: false
    },
    schedule: {
        start: {
            type: Date, 
            default: Date.now
        },
        end: {
            type: Date,
            default: Date.now
        }
    },
    submissions: {
        type: Number,
        default: 0
    }
});

解决方案

There's nothing built into Mongoose regarding migrating existing documents to comply with a schema change. You need to do that in your own code, as needed. In a case like the new enabled field, it's probably cleanest to write your code so that it treats a missing enabled field as if it was set to false so you don't have to touch the existing docs.

As far as the schema change itself, you just update your Schema definition as you've shown, but changes like new fields with default values will only affect new documents going forward.

这篇关于猫鼬更改架构格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆