0

I have fallowing schema:

var CountrySchema = new mongoose.Schema({
    name: String,
    relations : { type: Number, default: 0, min : 0, max : 1},    
});

var DiplomacySchema = new mongoose.Schema({
    year: { type: Number, default: 1900, min : 1900, max: 2020 },
    countries: [CountrySchema]
});

with the corresponding document in the database

{
   year: 1950,
   countries: [
      {
         name: "UK",
         relations: 0.5    
      },
      {
         name: "Germany",
         relations: 0.25    
      } 
   ]
}

I need to update UK current relations by some number, under constraint that updated value stays in the range boundaries. For example considering current UK relations is 0.5, update of +0.25 should lead to 0.75, update of -0.3 should result in 0.2, but update of +0.75 should be 1.

4

0 回答 0