$ne (Not Equals)
Last updated: 2026-10-09
$ne (Not Equals)
$ne selects documents where the field value does not equal the specified value — the negation of $eq. `{ age: { $ne: 18 } }` matches all documents where age is not 18. Like $eq it is type-sensitive and, counterintuitively, does NOT match documents missing the field.
| Category | Comparison |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $ne: <value> } }
⚙️ Parameters
| field | The document field to match. |
|---|---|
| value | The value to exclude. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $ne: exclude documents where age is 18
db.users.find({ age: { $ne: 18 } })
Output:
// Returns documents where age is not 18 (missing-field documents are excluded)
📌 Notes
- Does not match documents missing the field; combine with $exists: true to include them
- On arrays, matches if any element is not equal to the value
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QDoes $ne match documents where the field does not exist?
ANo. $ne matches only documents where the field exists and its value differs. To include missing fields, add $exists: true.
QWhat is the difference between $ne and $not: { $eq: ... }?
AFor scalar fields the results are identical. On arrays, $ne matches if any element differs, while $not: { $eq } requires no element to equal the value, which is stricter.
QCan $ne match null?
AYes. $ne: null excludes documents whose value is null, but documents where the field is missing are treated as not equal and match, so combine with $exists when needed.