Reputation: 386
I have this schema:
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
});
I receive new confing for my filters for example this object:
{
"inn": [
{
"name": "inn",
"sValue1": "1",
"sValue2": "",
"sOperation": "Contains",
"bExclude": false
},
{
"name": "inn",
"sValue1": "2",
"sValue2": "",
"sOperation": "Contains",
"bExclude": false
}
]
}
then perform this function
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"
});
}
});
}
where id is userId in schema and body is this new json objects with "inn" array. This query is successful but the data which was inserted in a wrong way. I get nested twice array. I expect that data will be replace. but it was embed into consisting empty array and not replaced it. Wrong is here: http://www.jsoneditoronline.org/?id=251509a941acb4adfe34c8a20414b50f
Upvotes: 1
Views: 310
Reputation: 45513
Use Array
type instead of Schema.Types.Array
:
var controlAgenciesAndObjectsFiltersSchema = new Schema({
userId: { type: Schema.Types.Number },
inn: { type : Array , "default" : [] }
});
or create an inn
Schema
and define an array of it :
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]
});
Upvotes: 1
Reputation: 10292
Why findAndUpdate
Just try with update
. It's working fine for me
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"
});
}
});
}
Read more from here about
Upvotes: 0