Strings, Booleans and Null
Someone searches the shop for "jane austen" and gets nothing, even though Emma is right there. Someone else marks signed books with the word "yes" and wonders why a filter for signed books misses half of them. Three everyday types, three everyday surprises.
The rule
Strings match exactly, booleans are not words, and null also means missing.
String: UTF-8 text, so any language or emoji is fine. Equality is exact and case-sensitive:
db.books.find({ author: "Jane Austen" }) // Emma
db.books.find({ author: "jane austen" }) // nothingBoolean: true or false, with no quotes. In our collection only The Hobbit has signed: true. The string "true" is a different value of a different type, so { signed: "true" } would not find it.
Null: an explicit "no value". The key rule: a query for null matches documents where the field is null or does not exist at all.
db.books.find({ signed: null })
// Dune, Neuromancer, Emma, Circe: none of them has a signed fieldTo tell the two cases apart, ask about the type instead. $type checks a field's BSON type by name:
db.books.find({ signed: { $type: "null" } }) // only fields actually set to null: none here
db.books.find({ title: { $type: "string" } }) // all five books