Skip to content

Conversation

@christophehurpeau
Copy link

Description

Summary of Changes

Allow $ne operator to accept null values.

null value is useful when we want to have a similar behavior as exists, but including null values even if the Value is not declared as nullable.

Notes for Reviewers

What is the motivation for this change?

Release Highlight

Release notes highlight

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

Allow $ne operator to accept null values. 

null value is useful when we want to have a similar behavior as exists, but including `null` values even if the Value is not declared as nullable.
@christophehurpeau christophehurpeau requested a review from a team as a code owner November 20, 2025 15:19
@tadjik1
Copy link
Member

tadjik1 commented Nov 25, 2025

Hi @christophehurpeau, thank you for the contribution! We are going to discuss this change internally as there are some usability and necessity questions arose. Can you maybe share the exact use-case or small snippet to demonstrate usage scenario, this would be extremely helpful.

@tadjik1 tadjik1 added External Submission PR submitted from outside the team tracked-in-jira Ticket filed in MongoDB's Jira system labels Nov 25, 2025
@christophehurpeau
Copy link
Author

@tadjik1

Of course.

Most of our models include an schema with non-nullable fields. Here a simple example:

interface EndUser {
  _id: Types.ObjectID;
  fullname: string;
  email?: string;
}

With an index on email.
When we want to query all enduser with existing emails:

const endUsers = await EndUserQueries.find({ email: { $ne: null } })

@dariakp dariakp changed the title Update $ne operator to include null type feat(NODE-7320): Update $ne operator to include null type Jan 9, 2026
@tadjik1 tadjik1 self-assigned this Jan 23, 2026
@tadjik1 tadjik1 added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Jan 23, 2026
@tadjik1
Copy link
Member

tadjik1 commented Jan 23, 2026

Hi @christophehurpeau,
thanks for providing that snippet - it makes the context and the goal of this change much clearer.

While I understand the convenience this would provide from the application side, we have to consider the driver’s role as a transparent interface to the database. There is a fundamental distinction between a field being null and a field being missing.
Since your schema defines email as optional (essentially string | undefined), the idiomatic way to query for this in MongoDB is using $exists, for example { email: { $exists: true } } - to find all users with email.
If you do have users with their emails null in the collection, it might be best to reflect that in your interface:

interface EndUser {
  ...
  email: string | null;
}

Please let me know if this helps!

@christophehurpeau
Copy link
Author

Hi @christophehurpeau, thanks for providing that snippet - it makes the context and the goal of this change much clearer.

While I understand the convenience this would provide from the application side, we have to consider the driver’s role as a transparent interface to the database. There is a fundamental distinction between a field being null and a field being missing. Since your schema defines email as optional (essentially string | undefined), the idiomatic way to query for this in MongoDB is using $exists, for example { email: { $exists: true } } - to find all users with email. If you do have users with their emails null in the collection, it might be best to reflect that in your interface:

interface EndUser {
  ...
  email: string | null;
}

Please let me know if this helps!

From what I understood, $exists did not use indexes. that's why we did not use it. It seems the bug was fixed, or for some other reason it works now, at least in the case I tested. I guess we can use $exists then. Thank you for your response.

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

Labels

External Submission PR submitted from outside the team Primary Review In Review with primary reviewer, not yet ready for team's eyes tracked-in-jira Ticket filed in MongoDB's Jira system

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants