how to auto update collection using mongoose

291 Views Asked by At

I want to auto update isActive field to false if timing.from is less than Date.now() and then filter out all those docs whose isActive is false. This can be achieved by mongodb-replica-sets. But it's not auto updating. Here is my code:

const schema = new mongoose.Schema({
  name: String,
  timing: {
    from: Date,
    to: Date,
  }, 
  isActive: {
    type: Boolean,
    default: true,
  },
});
const Concerts = mongoose.model("Concerts", schema);
const pipeline = [
  { $match: { "timing.from": { $lt: Date.now() } } },
  { $set: { isActive: false } },
];

Concerts.watch(pipeline).on("change", (data) => console.log(data));

And if you could also suggest how to filter on the basis of isActive on every find(). Thank you for your efforts.

0

There are 0 best solutions below