简体   繁体   English

TypeScript ZCCADCDEDB567ABAE643E15DCF0974E503Z 处理 Schema 和 DB 冲突

[英]TypeScript Mongoose handle Schema and DB conflict

I am quite new to MongoDB and mongoose and I'm not sure how to resolve conflicts with the schema and the database.我对 MongoDB 和 mongoose 很陌生,我不确定如何解决与架构和数据库的冲突。 By this I mean if I have an already established database and then I decide to expand on the schema in my project, adding defaults, how do I efficiently make those changes in the database as well?我的意思是如果我已经建立了一个数据库,然后我决定在我的项目中扩展模式,添加默认值,我如何有效地在数据库中进行这些更改?

Expanding a Mongoose schema typically doesn't require any explicit synchronisation with the database, because MongoDB itself is schemaless and doesn't really care about what you store in it.扩展 Mongoose 模式通常不需要与数据库进行任何显式同步,因为 MongoDB 本身是无模式的,并不真正关心您在其中存储的内容。

Things like defaults are handled by Mongoose itself, not by MongoDB, so adding defaults to your schema will work just fine.诸如默认值之类的事情由 ZCCADDEDB567ABAE643E15DCF0974E503Z 本身处理,而不是由 MongoDB 处理,因此将默认值添加到您的架构中就可以了。 However, because they are handled by Mongoose, this means that any data already stored in the database will not be updated if you set a default.但是,由于它们是由 Mongoose 处理的,这意味着如果您设置默认值,则不会更新已存储在数据库中的任何数据。 This also applies to changing the type of a field: if a field was previously a Number , and you change it to a String , existing database documents will not get changed.这也适用于更改字段的类型:如果字段以前是Number ,而您将其更改为String ,则现有数据库文档将不会更改。 But this usually doesn't present an issue.但这通常不会出现问题。

The only common change that I can think of that may require manual intervention is when your schema previously have fields marked as unique , and you remove the uniqueness constraint from your schema.我能想到的唯一可能需要手动干预的常见更改是,当您的架构以前具有标记为的字段时unique ,并且您从架构中删除了唯一性约束。 In such a case, you need to manually remove the unique index that was previously created by Mongoose (it will not remove it by itself).在这种情况下,您需要手动删除之前由 Mongoose 创建的唯一索引(它不会自行删除)。

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

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