mongodb 处理 Mongoose 中的架构更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7617002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:15:31  来源:igfitidea点击:

Dealing with schema changes in Mongoose

mongodbdata-migrationmongoose

提问by mahemoff

What's the best practice (or tool) for updating/migrating Mongoose schemas as the application evolves?

随着应用程序的发展,更新/迁移 Mongoose 模式的最佳实践(或工具)是什么?

采纳答案by Louis

Update:Tested, this does not work in its current form, its got the right idea going, I got a single migration to work with considerable tweaking of the module itself. But I don't see it working as intended without some major changes and keeping track of the different schemas somehow.

更新:经过测试,这在当前形式下不起作用,它的想法是正确的,我进行了一次迁移,可以对模块本身进行大量调整。但是我认为如果没有一些重大更改并以某种方式跟踪不同的模式,它就不会按预期工作。



Sounds like you want mongoose-data-migrations

听起来你想要猫鼬数据迁移

It is intended to migrate old schema versions of your documents as you use them, which is seems to be the best way to handle migrations in mongodb.

它旨在在您使用文档时迁移旧的模式版本,这似乎是在 mongodb 中处理迁移的最佳方式。

You don't really want to run full dataset migrations on a document collection (ala alter table) as it puts a heavy load on your servers and could require application / server downtime. Sometimes you may need to write a script which simply grabs all documents applies the new schema / alterations and calls save, but you need to understand when/where to do that. An example might be, adding migration logic to doc init has more of a performance hit than taking down the server for 3 hours to run migration scripts is worth.

您真的不想在文档集合(ala alter table)上运行完整的数据集迁移,因为它会给您的服务器带来沉重的负载,并且可能需要应用程序/服务器停机。有时您可能需要编写一个脚本来简单地抓取所有文档,应用新模式/更改并调用 save,但您需要了解何时/何地执行此操作。一个例子可能是,将迁移逻辑添加到 doc init 比关闭服务器 3 小时来运行迁移脚本对性能的影响更大。

I found this linkpretty helpful as well, basically reiterates the above in more detail and essentially implements the above node package's concept in php.

我发现这个链接也非常有用,基本上更详细地重申了上述内容,并且基本上在 php.ini 中实现了上述节点包的概念。

N.B.The module is 5months old, 0 forks, but am looking around and can't find anything better / more helpful than abdelsaid's style of response..

注意该模块已有 5 个月的历史,0 个叉子,但我环顾四周,找不到比 abdelsaid 的响应风格更好/更有用的东西..

回答by vimdude

It's funny though, MongoDB was born to respond to the schema problems in RDBMS. You don't have to migrate anything, all you have to do is set the default value in the schema definition if the field is required.

有趣的是,MongoDB 的诞生是为了应对 RDBMS 中的模式问题。您无需迁移任何内容,您只需在架构定义中设置默认值(如果需要该字段)。

new Schema({
    name: { type: string }
})

to:

到:

new Schema({
    name: { type: string },
    birthplace: { type: string, required: true, default: 'neverborn' }
});

回答by Aaron Pennington

I just had this problem where I needed to update my database to reflect the changes to my schema. After some research I decided to try the updateMany() function in the mongo console to make the updates and I think it worked pretty well.

我刚刚遇到了这个问题,我需要更新我的数据库以反映对我的架构的更改。经过一番研究,我决定在 mongo 控制台中尝试 updateMany() 函数来进行更新,我认为它运行得很好。

To apply this to vimdude's example the code would look as follows:

要将其应用于 vimdude 的示例,代码将如下所示:

try { 
    db.<collection>.updateMany( { birthplace: null }, { $set: 
              {"birthplace": "neverborn" } } ); 
} catch(e) { 
    print(e);
}

The updateMany() function will update all documents in a collection based on a filter. In this case the filter is looking for all documents where the field 'birthplace' is null. It then sets a new field in those documents named 'birthplace' and sets its value to 'neverborn'.

updateMany() 函数将根据过滤器更新集合中的所有文档。在这种情况下,过滤器正在查找“出生地”字段为空的所有文档。然后它在那些名为“birthplace”的文档中设置一个新字段,并将其值设置为“neverborn”。

After running the code, modified to reflect your circumstances run:

运行代码后,修改以反映您的运行情况:

db.<collection>.find().pretty()

to verify that the changes were made. The new field "birthplace" with the value of "neverborn" should show at the end of each document in your collection.

以验证是否进行了更改。值为“neverborn”的新字段“birthplace”应显示在您集合中每个文档的末尾。

Hope that helps.

希望有帮助。