node.js - Mongoose 查询使用嵌套数组更新对象

标签 node.js mongodb mongoose

我有这个架构:

var controlAgenciesAndObjectsFiltersSchema = new Schema({
  userId: { type: Schema.Types.Number },
  controlAgencyName: { type: Schema.Types.Array, default: [] },
  controlAgencyType: { type: Schema.Types.Array, default: [] },
  inn: { type: Schema.Types.Array, default: [] },
  ogrn: { type: Schema.Types.Array, default: [] },
  licenseNumber: { type: Schema.Types.Array, default: [] },
  licenseNumberRegDate: { type: Schema.Types.Array, default: [] },
  street: { type: Schema.Types.Array, default: [] },
  house: { type: Schema.Types.Array, default: [] },
  housing: { type: Schema.Types.Array, default: [] },
  building: { type: Schema.Types.Array, default: [] },
  contractNumberAndDate: { type: Schema.Types.Array, default: [] },
  controlStartDate: { type: Schema.Types.Array, default: [] },
  controlEndDate: { type: Schema.Types.Array, default: [] },
  houseType: { type: Schema.Types.Array, default: [] },
  cadastralNumber: { type: Schema.Types.Array, default: [] },
  constructionYear: { type: Schema.Types.Array, default: [] },
  commissioningYear: { type: Schema.Types.Array, default: [] },
  houseCondition: { type: Schema.Types.Array, default: [] },
  houseRunOutInPercents: { type: Schema.Types.Array, default: [] },
  houseSeriesAndProjectType: { type: Schema.Types.Array, default: [] },
  houseTotalArea: { type: Schema.Types.Array, default: [] },
  houseResidentalArea: { type: Schema.Types.Array, default: [] },
  interiorWallsType: { type: Schema.Types.Array, default: [] },
  energyEfficiencyClass: { type: Schema.Types.Array, default: [] },
  energyInspectionDate: { type: Schema.Types.Array, default: [] },
}, {
  minimize: false,
  versionKey: false
});

我收到了新的过滤器配置,例如这个对象:

{
"inn": [
  {
    "name": "inn",
    "sValue1": "1",
    "sValue2": "",
    "sOperation": "Contains",
    "bExclude": false
  },
  {
    "name": "inn",
    "sValue1": "2",
    "sValue2": "",
    "sOperation": "Contains",
    "bExclude": false
  }
]
}

然后执行这个函数

function updateCustomConfig (model, response, id, body) {
  model.findOneAndUpdate({ userId: id }, { $set: body }, function (err, doc) {
    if (err) {
      response.send({
        status: "error"
      });
    } else {
      response.send({
        status: "success"
      });
    }
  });
}

其中 id 是 schema 中的 userId,body 是带有“inn”数组的新 json 对象。查询成功,但插入的数据错误。我嵌套了两次数组。我预计数据将被替换。但它被嵌入到组成的空数组中并且没有替换它。 错误在这里:http://www.jsoneditoronline.org/?id=251509a941acb4adfe34c8a20414b50f

最佳答案

使用Array类型而不是Schema.Types.Array:

var controlAgenciesAndObjectsFiltersSchema = new Schema({
    userId: { type: Schema.Types.Number },
    inn: { type : Array , "default" : [] }
});

或者创建一个inn Schema并定义它的一个数组:

var innSchema = new Schema({

    "name": String,
    "sValue1": String,
    "sValue2": String,
    "sOperation": String,
    "bExclude": Boolean

}, { _id: false });

var controlAgenciesAndObjectsFiltersSchema = new Schema({
    userId: { type: Schema.Types.Number },
    inn: [innSchema]
});

关于node.js - Mongoose 查询使用嵌套数组更新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41314121/

相关文章:

mongodb - 获取mongodb中单个查询的最小值和最大值

mysql - 如何将流氓查询转换为Mysql?

javascript - 使用 Mongoose 时出现错误 'Object has no method xxx '

node.js - 如何使用 Mongoose 在某个日期自动删除 MongoDB 中的文档

angularjs - Express 防止 SPA 中的重定向

javascript - 可能的 Controller 或至少执行两次的函数

node.js - mongoose在查询时降低内存使用的方法

javascript - Angular +图像上传。将表单数据传输到后端时遇到问题

node.js - 如何通过rabbitmq发送带有附加数据的图片?

events - 如何在 node.js (express.js) 中创建自定义事件监听器?