Skip to content

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

Open
luizcarlos1405 wants to merge 1 commit into
hokify:mainfrom
luizcarlos1405:main
Open

Set fields to delete explicitly to null instead of undefined#45
luizcarlos1405 wants to merge 1 commit into
hokify:mainfrom
luizcarlos1405:main

Conversation

@luizcarlos1405

@luizcarlos1405 luizcarlos1405 commented Sep 1, 2023

Copy link
Copy Markdown

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