The Trainer
The Trainer

Reputation: 695

mongoose deleting and updating in array

I need to take array from my model, delete from it some days and push to it some other days. It looks something like deleteAndUpdate.

To sum up: I need to take Car from database. Take reserved property (it's a array), then delete from reserved days from given list, and then add to reserved days from other given list.

My model look:

const CarSchema = mongoose.Schema({
    mark:{
        type: String,
        required: true,},
    model:{
        type: String,
        required: true,},
    price:{
        type: Number,
        required: true,},
    available: {
        type: Boolean,
        required: true,},
    reserved:{
        type:[Date],

    },
    pic_1:{
        type:String,
        required:true,
    },
    pic_2:{
        type:String,
        required:true,
    },


},
{ collection: 'cars' }
)

I take car by: var car= await Car.findById(carID); and then i need to do sth like that:

car['reserved'].deleted(old_days);
car['reserved'].push(new_days;
car.save();

Could someone help me?

Upvotes: 1

Views: 1020

Answers (2)

turivishal
turivishal

Reputation: 36104

Update can't allow multiple operation at a time in same field, It will throw multiple write error and would create a conflict at your field,

Regular update:

If you want to do it by regular update query you have to do separate do 2 queries,

  • Delete days: If you want to delete multiple days use $pullAll, and for single you can use $pull
var old_days = [new Date("2021-04-24"), new Date("2021-04-25")];
await Car.updateOne({ _id: carID }, { $pullAll: { reserved: old_days } });
  • Add days: if you want to add multiple days you can use $push with $each, and for single you can use just $push,
var new_days = [new Date("2021-04-26"), new Date("2021-04-27")];
await Car.updateOne({ _id: carID }, { $push: { reserved: { $each: new_days } } });

Update with aggregation pipeline:

If you are looking for single query you can use update with aggregation pipeline starting from MongoDB 4.2,

  • $filter to iterate loop of reserved array and remove old days
  • $concatArrays to concat reserved array with new days
var old_days = [new Date("2021-04-24"), new Date("2021-04-25")];
var new_days = [new Date("2021-04-26"), new Date("2021-04-27")];
await Car.updateOne(
  { _id: carID },
  [{
    $set: {
      reserved: {
        $filter: {
          input: "$reserved",
          cond: { $not: { $in: ["$$this", old_days] } }
        }
      }
    }
  },
  {
    $set: {
      reserved: {
        $concatArrays: ["$reserved", new_days]
      }
    }
  }]
);

Playground

Upvotes: 1

pshrimal000
pshrimal000

Reputation: 97

To remove old item from array you can use $pull

car.update(
  { _id: carID },
  { $pull: { 'reserved': old_days } }
);

You can use $unset to unset the value in the array (set it to null), but not to remove it completely.

To add the item new_days in array, You can either use $push or $addToSet

car.update(
  { _id: carID },
  { $push: { 'reserved': new_days } }
);

Upvotes: 1

Related Questions