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:
Is it officially supported and safe to use true/false values in projection documents long-term? Is there any risk that future MongoDB versions may stop treating true and 1 the same way? 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's officially supported, and I don't think MongoDB developers will break the behaviour of something so basic as the boolean.
I understand you are worried about thruty/falsy values. But, I 100% don't think a boolean false
will be treated as an empty string or 0 in Go, because of its strict typed nature. A boolean true
will always be true
; where false
will always be false
in Go.
Moreover, If you are working for a long term, and possibly growing database that will get bigger over time, then booleans are definitely better because they are more compact and efficient for storage, especially when compared to using strings or numbers to represent switch boolean states.