我有这样一个模式:你如何通过mongoose方法更改fieldName并在模式中更改它?
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) { });
或者我不在代码中这么做?我从来没有处理数据库模式更改,所以我不确定如何/在哪里应用模式更改。
这是一个代码部署问题代替字段名先更换。要在运行时更改模型上的模式,您需要取消注册当前模型,然后再次注册新模式更改。如果您所做的只是“部署生产更改”,那么您应该立即停止程序访问并更新实际数据,然后使用更改后的代码重新启动。 –