mongodb-cron icon indicating copy to clipboard operation
mongodb-cron copied to clipboard

Broken with MongoDB version 6

Open timb-103 opened this issue 9 months ago • 0 comments

In mongo version 6, they have made breaking changes to findOneAndUpdate (https://github.com/mongodb/node-mongodb-native/blob/main/etc/notes/CHANGES_6.0.0.md#findoneandx-family-of-methods-will-now-return-only-the-found-document-or-null-by-default-includeresultmetadata-is-false-by-default).

Now, it simply returns the document without any metadata, so in the following res.value will be undefined, it simply needs to return res.

Also, returnOriginal has been removed and should be replaced with returnDocument: 'before'

  protected async lockNext() {
    const sleepUntil = moment().add(this.config.lockDuration, 'milliseconds').toDate();
    const currentDate = moment().toDate();

    const res = await this.getCollection().findOneAndUpdate({
      $and: [
        { [this.config.sleepUntilFieldPath]: { $exists: true, $ne: null }},
        { [this.config.sleepUntilFieldPath]: { $not: { $gt: currentDate } } },
        this.config.condition,
      ].filter((c) => !!c),
    }, {
      $set: { [this.config.sleepUntilFieldPath]: sleepUntil },
    }, {
      returnOriginal: true, // return original document to calculate next start based on the original value
    });
    return res.value;
  }

If you could please update this so we can upgrade to mongo 6 that would be appreciated! Thanks.

timb-103 avatar May 04 '24 00:05 timb-103