$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.

CategoryComparison
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ <field>: { $ne: <value> } }

⚙️ Parameters

fieldThe document field to match.
valueThe 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏