1

I want to update chargeList isDelete property to true when date is greater than a specific value. my schema as follows.

{
   "chargeList": [
     { 
         "date": ISODate("2013-06-26T18:57:30.012Z"),
         "price": "123",
         "isDelete": false

     },
     { 
         "date": ISODate("2013-06-27T18:57:30.012Z"),
         "price": "75",
         "isDelete": false

     }
   ]
 }

schema is as follows.

var ChargeListSchema= new Schema({
    date:  { type: Date , default: Date.now  },
    price: { type: String, required: false },
    isDelete: { type: Boolean, default: false }
});

var ScheduleChargeSchema = new Schema({

  chargeList:[ChargeListSchema]


  });

I have tried as following code but it only update the matching first element in chargeList array.

 Model.update(
  {
    "_id": 1,
    "chargeList": {
      "$elemMatch": {
        "date": {
           $gt:  ISODate("2013-06-26T18:57:30.012Z")
        }
      }
    }
  },
  {
    "$set": { "chargeList.$.isDelete": true }
  }
)
Ashh
  • 44,693
  • 14
  • 105
  • 132
Manoj Sanjeewa
  • 1,069
  • 1
  • 11
  • 34

1 Answers1

1

You need to use $[] all positional operator to update multiple elements in an array

Model.update(
  { "_id": 1, "chargeList.date": { "$gt":  ISODate("2013-06-26T18:57:30.012Z") }},
  { "$set": { "chargeList.$[].isDelete": true } }
)
Ashh
  • 44,693
  • 14
  • 105
  • 132