【发布时间】:2018-02-24 03:16:27
【问题描述】:
我有这样的架构:
const StorySchema = new Schema({
authorId: String,
title: String,
longUrl: String,
shortUrl: String,
comment: String,
category: String,
author: String,
date: { type: Date, default: Date.now, index: true },
votes: { type: Number, default: 1, index: true },
});
我想将架构上的投票字段更改为 votesCount,同时我想实际更改架构。
我会在同一个文件中执行这些操作吗?
const StorySchema = new Schema({
authorId: String,
title: String,
longUrl: String,
shortUrl: String,
comment: String,
category: String,
author: String,
date: { type: Date, default: Date.now, index: true },
votesCount: { type: Number, default: 1, index: true },
});
const StoryModel = mongoose.model('story', StorySchema);
StoryModel.update({}, { $rename: { votes: 'votesCount' } }, { multi: true, strict: false }, function(err, blocks) { });
或者我在代码中根本不这样做?我从未处理过数据库架构更改,所以我不确定如何/在何处应用架构更改。
【问题讨论】:
-
这是一个代码部署问题。要在运行时更改模型上的模式,您需要取消注册当前模型,然后再次注册新的模式更改。如果您所做的只是“将更改部署到生产环境”,那么您应该立即停止程序访问并更新实际数据,然后使用更改后的代码重新启动。