Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用嵌套数组的更新对象的Mongoose查询_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 使用嵌套数组的更新对象的Mongoose查询

Node.js 使用嵌套数组的更新对象的Mongoose查询,node.js,mongodb,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:

我有这个模式:

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是模式中的userId,body是带有“inn”数组的新json对象。此查询成功,但数据以错误的方式插入。我得到两个嵌套数组。我预计数据将被替换。但它是嵌入到空数组中的,而不是替换它。
这里有错误:

为什么
查找和更新

只需尝试使用
更新
。这对我来说很好

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

阅读更多关于使用
Array
类型而不是
Schema.Types.Array

var controlAgenciesAndObjectsFiltersSchema = new Schema({
    userId: { type: Schema.Types.Number },
    inn: { type : Array , "default" : [] }
});
或者创建一个
inn
模式
并定义其数组:

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]
});

谢谢数组而不是Schema.Types.Array帮助了我很多!你能解释一下吗?为什么这样做?老实说,我不知道为什么
Schema.Types.Array
的行为不像
Array
那样是一种类型。在文档中,没有使用
Schema.Types.Array
。因此,我想应该在mongoose模块内部使用
Schema.Types.Array
,以及
Schema.Types.DocumentArray