How does a boolean perform relative to an int in mongoDB when the field can only have one of two values?
Specifically, I have a collection storing objects that may or may not still be active. So, I want to have an 'active' key for each row. Better to input
{..., active: false, ...}
or
{..., active: 0, ...}
I'm more interested in performance in terms of speed when querying for the active objects, but I'm interested secondarily in how much space will be used--in case there is a tradeoff.
When you are interested in low-level details about how certain data-types are implemented, the best reference is the BSON specification.
There you will see that a boolean is two bytes (one byte for type information "boolean" and another byte 0x00
for "false" and 0x01
for "true") while a 32bit integer has 5 bytes (1 byte for the type prefix and 4 byte for the 32bit content). BSON also supports 64bit integers which takes 9 bytes, but in most programming languages the driver will store a 32bit integer unless you explicitly use a 64bit integer type. Remember that the name of the key is a string which also takes up space.
Query performance is unlikely to be affected much by which type you use. A query has far more time-consuming things to do than the one CPU operation it takes to compare two bytes / 32bit integers.