HOWTO · MongoDB
How to Query for Is Not Null Value in MongoDB
Query MongoDB for documents where a field is not null: $ne: null matches fields that exist and hold a value, $type and $exists separate an actual null from a missing field, and a second $ne test excludes empty strings.
On this page
MongoDB has no IS NOT NULL keyword, so the SQL test has to be expressed with query operators. The operator that does the job is $ne: as the MongoDB manual puts it, { field: { $ne: null } } matches documents where the field exists and has a non-null value. This guide builds a small sample collection and walks through the not-null query on a string field and a numeric field, the opposite equality test, the difference between a null value and a missing field, the empty-string trap, and $eq for one specific value.
Set Up the Sample Collection
The examples use a teams collection with seven documents. Three of them carry the cases that matter: Liverpool has position: null, Arsenal has points: null, and Everton has no position field at all.
db.teams.insertMany([
{ team: "Manchester City", position: "1st", points: 70 },
{ team: "Liverpool", position: null, points: 69 },
{ team: "Chelsea", position: "3rd", points: 59 },
{ team: "Arsenal", position: "4th", points: null },
{ team: "Tottenham", position: "5th", points: 51 },
{ team: "Manchester United", position: "6th", points: 50 },
{ team: "Everton", points: 31 }
])
The { _id: 0 } projection in the queries below only hides the generated _id values so the output stays readable.
Query for Not Null Values With the $ne Operator
To find the teams whose position is not null, compare the field against null with $ne:
db.teams.find({ position: { $ne: null } }, { _id: 0 })
Output:
[
{ team: 'Manchester City', position: '1st', points: 70 },
{ team: 'Chelsea', position: '3rd', points: 59 },
{ team: 'Arsenal', position: '4th', points: null },
{ team: 'Tottenham', position: '5th', points: 51 },
{ team: 'Manchester United', position: '6th', points: 50 }
]
Liverpool is missing because its position is null. Everton is missing too, and that is the part readers most often get wrong: for a comparison against null, MongoDB groups a null value and a missing field together (the equality test in the next section matches both), so $ne: null returns the complement — only documents where the field exists and holds a value. The same test works on a numeric field. To find the teams whose points is not null:
db.teams.find({ points: { $ne: null } }, { _id: 0 })
Output:
[
{ team: 'Manchester City', position: '1st', points: 70 },
{ team: 'Liverpool', position: null, points: 69 },
{ team: 'Chelsea', position: '3rd', points: 59 },
{ team: 'Tottenham', position: '5th', points: 51 },
{ team: 'Manchester United', position: '6th', points: 50 },
{ team: 'Everton', points: 31 }
]
This time only Arsenal drops out, because points is null there. Everton stays in the result: its points field exists and holds a value, and the missing position field is irrelevant to this query.
Find Null and Missing Values With an Equality Filter
The natural counterpart of the not-null test is the equality filter { field: null }. It matches documents where the field is null or where the field does not exist at all:
db.teams.find({ position: null }, { _id: 0 })
Output:
[
{ team: 'Liverpool', position: null, points: 69 },
{ team: 'Everton', points: 31 }
]
The filter returns Liverpool (an actual null) and Everton (no position field) together. Writing the same test as { position: { $eq: null } } behaves identically — which is also why $eq by itself is not a not-null tool: with null it runs this equality test, and with any other value it simply matches that value, as the last section shows.
Tell an Actual Null Apart From a Missing Field
When the difference between “null and missing” matters, use $type or $exists. The BSON type number for null is 10, so this query returns only documents whose position is actually null:
db.teams.find({ position: { $type: 10 } }, { _id: 0 })
Output:
[ { team: 'Liverpool', position: null, points: 69 } ]
And this query returns only the documents that have no position field at all:
db.teams.find({ position: { $exists: false } }, { _id: 0 })
Output:
[ { team: 'Everton', points: 31 } ]
Exclude Empty Strings as Well
A null test says nothing about empty strings: a document can hold position: "", which is a real value and passes $ne: null. Add two such documents:
db.teams.insertMany([
{ team: "West Ham", position: "", points: 48 },
{ team: "Wolves", position: "", points: 46 }
])
The tempting shortcut is to test against the empty string instead:
db.teams.find({ position: { $ne: "" } }, { _id: 0 })
Output:
[
{ team: 'Manchester City', position: '1st', points: 70 },
{ team: 'Liverpool', position: null, points: 69 },
{ team: 'Chelsea', position: '3rd', points: 59 },
{ team: 'Arsenal', position: '4th', points: null },
{ team: 'Tottenham', position: '5th', points: 51 },
{ team: 'Manchester United', position: '6th', points: 50 },
{ team: 'Everton', points: 31 }
]
That returns seven documents, including Liverpool (null) and Everton (missing): $ne: "" only rejects the two empty strings, because every other value — null and a missing field included — is “not equal to” "". To require a value that is neither null nor empty, combine both $ne tests with $and:
db.teams.find(
{ $and: [{ position: { $ne: null } }, { position: { $ne: "" } }] },
{ _id: 0 }
)
Output:
[
{ team: 'Manchester City', position: '1st', points: 70 },
{ team: 'Chelsea', position: '3rd', points: 59 },
{ team: 'Arsenal', position: '4th', points: null },
{ team: 'Tottenham', position: '5th', points: 51 },
{ team: 'Manchester United', position: '6th', points: 50 }
]
Match One Specific Value With $eq
Finally, $eq is an equality operator: it matches documents where the field equals the given value. It is the right tool when you already know the value you want — for example, the team in 3rd position:
db.teams.find({ position: { $eq: "3rd" } }, { _id: 0 })
Output:
[ { team: 'Chelsea', position: '3rd', points: 59 } ]
The shorthand { position: "3rd" } means the same thing. Just do not reach for $eq as a null test: $eq: null runs the equality filter from the earlier section and returns null-or-missing documents, the opposite of this guide’s goal.
Conclusion
To query for “is not null” in MongoDB, use { field: { $ne: null } }: it matches exactly the documents where the field exists and holds a value. Keep the neighbouring tests straight — { field: null } finds null-or-missing documents, $type: 10 finds only actual nulls, $exists: false finds only missing fields, and empty strings need their own extra $ne test.