Mongodb:无法在嵌套对象中添加新密钥

时间:2017-06-06 14:47:54

标签: mongodb mongoose

我在mongodb中有一个架构,如

var schema = new mongoose.Schema({
  agentCode: String,
  data: {
    type: Object,
    profDtl: {
      education: String      
    }
});

现在我想在desgnName

中添加新属性profDtl
var schema = new mongoose.Schema({
  agentCode: String,
  data: {
    type: Object,
    profDtl: {
      desgnName: String, // trying to add new property
      education: String      
    }
});

但它没有反映在数据库中

1 个答案:

答案 0 :(得分:0)

我得到了一个解决方案,每当在mongodb架构中添加新属性时,它都需要一个默认值来反映新条目

表示例如:

var schema = new mongoose.Schema({
  agentCode: String,
  data: {
    type: Object,
    profDtl: {
      desgnName: {
        type: String,
        default: ""
      },
      education: String      
    }
});

现在工作正常