1

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

2 Answers 2

1

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]
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! Array instead Schema.Types.Array helped me a lot! Can you explain this? Why this works?
Honestly, I don't know why Schema.Types.Array doesnt behave like Array as a type. From the doc mongoosejs.com/docs/schematypes.html, there is no usage of Schema.Types.Array. So, I guess Schema.Types.Array should be used internally within mongoose module as well as Schema.Types.DocumentArray
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.