简体   繁体   English

猫鼬更改架构字段类型

[英]Mongoose Change Schema Field Type

How to change the type of one field in Mongoose? 如何更改猫鼬中一个字段的类型?

For example: 例如:

var FooSchema = new Schema({
  fooDate: {
    type: String,
    unique: true,
    required: true,
    trim: true
  }
}

fooDate is a string type, I want to change it to Date type? fooDatestring类型,我想将其更改为Date类型吗?

If I just change it, what will happen to the existing data? 如果仅更改它,现有数据将如何处理? What's the proper way to migrate the old data? 迁移旧数据的正确方法是什么?

Thanks. 谢谢。

Well it would depend on how do you have your date stored as string. 好吧,这取决于您如何将日期存储为字符串。 Is it a valid date ? 这是有效日期吗? Lets assume that it is either in valid ISO format or something like this: 假设它采用有效的ISO格式或类似的格式:

"2019-03-20"  or "2019-03-20T09:15:37.220Z"

If you simply change from String to Date mongoose would convert that for you and would treat that as the new type. 如果您简单地从String更改为Date猫鼬会为您转换它,并将其视为新类型。 So you would not need to migrate per se your date. 因此,您本身就不需要迁移日期。 When you get a model its fooDate would now be Date vs String before. 当您得到一个模型时,它的fooDate现在将是Date vs String

However the main issue you are facing now is querying data that is in two formats. 但是,您现在面临的主要问题是查询两种格式的数据。 Because any new records now would be saved as dates vs strings. 因为现在所有新记录都将另存为日期与字符串。

So you would be able to get by with the type change but while that would buy you some time you would need to migrate your data to reduce the friction during querying later. 这样您就可以解决类型更改的问题,但这虽然会给您带来一些时间,但是您需要迁移数据以减少以后查询时的麻烦。 Migrating a single field would be a trivial migration script so there is nothing scary there. 迁移单个字段将是一个琐碎的迁移脚本,因此那里没有什么令人恐惧的地方。

PS . PS You can always test this btw. 您可以随时测试此操作。 I just did with a simple schema and a saved record which had date as String then I updated the schema and did a simple Model.find to get that record and validate that now the date is an actual date and there was no issue with the model from mongoose. 我只是用一个简单的模式和一个保存的记录( dateString然后更新了该模式并执行了一个简单的Model.find以获取该记录并验证现在该date是实际日期,并且该模型没有问题来自猫鼬。 As long as the date string was in the correct date format. 只要日期字符串采用正确的日期格式。

You can change the data type any time .eg 您可以随时更改数据类型。

var FooSchema = new Schema({
  fooDate: {
    type: Date,
    unique: true,
    required: true,
    trim: true
  }
}

Now the new document records will be enter according to new data type. 现在,将根据新的数据类型输入新的文档记录。 for existing or previous one it depends how you store a string you should make a script either in python or nodejs it depends upon you. 对于现有的或先前的脚本这取决于您如何存储字符串 ,应该使用python还是nodejs 编写脚本 ,具体取决于您。 first fetching the all records and make a helper function that take string and convert the string into data of that time you could easily do in javascript using Date Object as Date('2014-08-12') 首先获取所有记录,并创建一个辅助函数,将字符串转换为该时间的数据,您可以轻松地在JavaScript中使用Date Object作为Date('2014-08-12')

Then you update all the previous records that you fetched in this way you can do consistency 然后,您可以更新以这种方式获取的所有以前的记录,从而实现一致性

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

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