Skip to the workspace
Pebellslearn by building
Data Model & Data Types  ›  Lesson

Strings, Booleans and Null

String 12 minStrings are UTF-8 text matched exactly and case-sensitively, booleans are true or false rather than text, and a query for null matches both explicit nulls and missing fields.

mongodb / Strings, Booleans and Null

Explore each scene at your own pace.

01 · The rule

The rule

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" })   // nothing

Boolean: 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 field

To 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
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.