在猫鼬中创建新的模型字段

时间:2019-06-27 06:36:35

标签: node.js mongoose

所以最近我有一个模型 customer.js ,该模型具有以下字段:

   const customer = new schema({
    name:String,
    email:String,
    age:Number,
    });

几个月后,现在我在 description 中添加了新字段。我的问题是,是否要将这个新模型投入生产。它会如何影响我以前的模型以及使用旧模型创建的客户?恐怕如果将这个新产品投入生产,它会在UI控制台中引发错误description undefined,因为老客户没有这样的字段描述?

1 个答案:

答案 0 :(得分:2)

为避免破坏事物:

  1. 使用新的description字段更新架构:
const customer = new schema({
  name: String,
  email: String,
  age: Number,
  description: String,
});
  1. 运行db.collection.update(...)以确保您不会破坏工作,并且每个客户都具有相同的架构:
db.customers.updateMany(
  { description: { $exists: false } } // All the customers without a description
  { $set: { description: '' } } // Set the description field to an empty string
)
相关问题