Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set fields to delete explicitly to null instead of undefined #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

luizcarlos1405
Copy link

@luizcarlos1405 luizcarlos1405 commented Sep 1, 2023

I use Meteor at work, and it configures the MongoDB drivers to ignore undefined values when updating a field instead of setting the field to null (unlike the MongoDB driver, which does not ignore undefined by default).

This behavior caused a specific issue where the same job document was repeatedly fetched, resulting in a job running in a loop until the lock is released due to the end of the lock time. The document would match because of the second condition in this $or query, as the lockedAt field wasn't set to null as expected:

const JOB_PROCESS_WHERE_QUERY: Filter<IJobParameters /* Omit<IJobParameters, 'lockedAt'> & { lockedAt?: Date | null } */> =
{
name: jobName,
disabled: { $ne: true },
$or: [
{
lockedAt: { $eq: null as any },
nextRunAt: { $lte: nextScanAt }
},
{
lockedAt: { $lte: lockDeadline }
}
]
};

However, explicitly setting the fields to null instead of undefined is a better practice because, in the end, they turn into null in the database. Also, not ignoring undefined and turning it into null feels weeeiiiird.

If someone else is facing the same issue, a possible fix is adding this option to your settings.json file:

"packages": {
    "mongo": {
        "options": {
            "ignoreUndefined": false
        }
    }
}

This configuration ensures that setting a field to undefined actually sets it to null. But please note that if you have code relying on the previous behavior, this fix could potentially break something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants