我有这个架构:
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是这个新的json对象,带有" inn"阵列。此查询成功但是以错误的方式插入的数据。我得到两次嵌套数组。我希望数据将被替换。但它被嵌入到组成的空数组而不是替换它。 这里错了:http://www.jsoneditoronline.org/?id=251509a941acb4adfe34c8a20414b50f
答案 0 :(得分:1)
使用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]
});
答案 1 :(得分:0)
为什么findAndUpdate
试试update
。它对我来说很好用
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"
});
}
});
}
从here了解更多