简体   繁体   English

用 NodeJS 重构猫鼬集合模型

[英]Restructuring mongoose collection model with NodeJS

I have created a MongoDB collection with mongoose model using express (node.js) and it is working well.我使用 express (node.js) 创建了一个带有猫鼬模型的 MongoDB 集合,并且运行良好。 And now I want to restructure the model of the same collection.现在我想重构同一个集合的模型。 For that I have to delete the collection in the database and restart the server so that it is updating with new model.为此,我必须删除数据库中的集合并重新启动服务器,以便它使用新模型进行更新。 But I have live data which is in use.但我有正在使用的实时数据。 So how can I restructure the collection without deleting it.那么如何在不删除它的情况下重组集合。

Do we have any other way to resolve this issue which is not to delete the collection and restart the server to update the mongoose model in the collection?除了删除集合并重新启动服务器以更新集合中的猫鼬模型之外,我们还有其他方法可以解决此问题吗?

My old Model looks like this:我的旧模型如下所示:

new mongoose.Schema({
name: {type:String},
age: {type: String, required: true}
});

And I want this new model to be updated in the same collection:我希望在同一个集合中更新这个新模型:

new mongoose.Schema({
name: {type:String, unique: true},
age: {type: Number, required: true},
grade: {type:Number}
});

您可以创建一个运行现有集合的 cron,并使用新字段更新旧文档。

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

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