Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Model & Data Types  ›  Capstone Project

Rare Types and the Sort Order

Max Key 15 minBSON also has binary data, regular expressions, MinKey and MaxKey plus a few deprecated types, and when values of different types are compared MongoDB uses a fixed type order from MinKey up to MaxKey.

Rare Types and the Sort Order

Most fields you design will be strings, numbers, booleans, dates, objects and arrays. The remaining BSON types are rarer, but you will meet them in other people's data, and one idea from this lesson, the type order, explains some of the strangest query results you will ever see.

The rule

Every type has a rank: MinKey lowest, MaxKey highest, everything else between.

The rarer types, briefly:

  • Binary data (BinData): raw bytes, such as a small thumbnail or a hash. A subtype marks what they are; subtype 4 is a UUID, created in mongosh with UUID().
  • Regular expression: a pattern. You mostly use it in queries: { title: /^The/ } finds The Hobbit. Patterns are case-sensitive unless you add the i flag.
  • JavaScript (code): a stored function. Rare today; do not keep app logic in documents.
  • Symbol and Undefined: deprecated leftovers. Do not create them; treat them as data to clean up.
  • MinKey and MaxKey: special values that compare below and above every other value. MongoDB uses them internally, for example to mark the open ends of a range.

When MongoDB compares values of different types, it ranks the types in this order, lowest first:

MinKey → Null → Numbers → String → Object → Array → Binary data → ObjectId → Boolean → Date → Timestamp → Regular expression → MaxKey

A missing field sorts like null. All numeric types share one rank and compare by value.

The shortcut

For a sort on mixed data, bucket the values by type first, then sort inside each bucket. For a range query like $gt, only the value's own bucket is searched.

Sorting a messy year fieldStep 1 of 4
  1. The values

    One book has year: null, two have numbers (1937, 1965), one has the string "1815".

The same buckets explain the range surprise: { year: { $gt: 1900 } } compares with numbers only, so the string year is never considered.

Drills

Answer fast — trust the rule.

quick drill

Which BSON value compares lower than every other value?

quick drill

Sorting ascending, which comes first?

quick drill

How many books does db.books.find({ title: /e$/ }) return?

quick drill

Which of these types should you avoid creating because it is deprecated?

quick drill

A document stores year: "1815" as a string. Does { year: { $lt: 1900 } } match it?

Remember

  • Every type has a rank: MinKey, null, numbers, strings … up to MaxKey.
  • Shortcut: bucket by type first, then sort within each bucket; ranges stay in one bucket.
  • Trap: mixed types in one field make sorts and ranges silently wrong; fix the data with $type.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainMixed types in one field

After a messy import, some books store year as a number, some as a string such as "1965", and one has year: null. Explain what order you get from db.books.find().sort({ year: 1 }), why { year: { $gt: 1900 } } misses the string years, and how you would find and fix them.