Using true/false vs 1/0 in MongoDB Projections — Long-Term Compatibility

Hi Everyone,

In our Go codebase, we’re building dynamic projection maps for queries. Instead of using map[string]int{"name": 1}, we’re considering using map[string]bool{"name": true} for cleaner and more expressive code.

We understand that MongoDB currently treats true and 1 equivalently in projections. However, we’d like to confirm:

  1. Is it officially supported and safe to use true/false values in projection documents long-term?
  2. Is there any risk that future MongoDB versions may stop treating true and 1 the same way?
  3. Does the MongoDB engine or Go driver perform any significant conversion overhead when using bool values in projection?

Thanks in advance — we just want to make the cleanest and safest long-term choice.

Yes, it should be totally safe, in fact internally if you look at some explain plans you can see that PROJECTION_SIMPLE stage uses true/false and 1/0 somewhat interchangeably. While that might be changed to be more consistent in output in the future, both will be parsed as equivalent.

Asya

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.