$bitNot (Bitwise NOT)

Last updated: 2026-10-09

$bitNot (Bitwise NOT)

$bitNot performs a bitwise NOT (two's complement) on a single integer — the only bitwise operator that takes one operand. Available since MongoDB 6.3, the result equals -x-1, so NOT 5 gives -6. It is used to build complementary masks and general negation in bit math. A null or missing argument yields null; the operand must be int32 or int64. Note the sign flips: this is two's complement, not a plain inversion of magnitude.

CategoryMath
MongoDB Version6.3+
Example Languagemongosh (Shell)

📝 Syntax

{ $bitNot: <integer-expression> }

⚙️ Parameters

integerAn int32 or int64 integer, or an expression resolving to one.

Returns:The bitwise NOT of the input (-x-1); null when null or missing

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $bitNot: invert permission bits to build an exclusion mask
db.users.aggregate([
  { $project: {
      denied: { $bitNot: '$allowedBits' }
  } }
])

Output:

{ '_id': 1, 'denied': -6 }
{ '_id': 2, 'denied': -1 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat is $bitNot(5)?
AIt is -6, since two's-complement NOT follows -x-1.
QWhy does $bitNot produce negative results?
ATwo's-complement NOT flips the high bit, changing the sign.
QWhere is $bitNot useful?
ABuilding complementary masks or composing with other bitwise operators.
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%

🙏 帮我们做得更好

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

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