Ch. 12 · MongoDB

MongoDB Collation and Case-Insensitive Queries

Use collations for case- and accent-insensitive matching, and make the index collation match the query collation.

~2 min readadvancedupdated Oct 5, 2026

A collation defines language-specific comparison rules, such as ignoring case or accents. Without one, MongoDB compares bytes, so Ann@example.com and ann@example.com differ. Collations let you match and sort the way users expect, but the index and the query must use the same collation.

Before you start

You should be comfortable with find, sorting and indexes. This article uses the MongoDB shell.

Step-by-step walkthrough

Step 1: Pick the strength that matches intent

A collation’s strength controls how much it ignores: strength 1 ignores case and accents, strength 2 ignores case but not accents. Choose the lowest strength that gives the behavior you want, since weaker comparisons match more values and affect uniqueness.

Step 2: Use the same collation on query and index

Create the index with the collation and pass the same collation to the query: find(...).collation({ locale: 'en', strength: 2 }). If they differ, the index cannot serve the query and MongoDB falls back to a scan, which is a common and silent performance loss.

Step 3: Account for collation in uniqueness

A unique index with a case-insensitive collation treats A and a as equal, so it prevents duplicates that differ only by case. That is usually desired for emails and usernames, but it changes what uniqueness means, so decide deliberately.

Worked scenario

The index and query share a case-insensitive collation.

db.users.createIndex({ email: 1 }, { collation: { locale: 'en', strength: 2 } });
db.users
  .find({ email: 'Ann@example.com' })
  .collation({ locale: 'en', strength: 2 });
JavaScript

Walk through the example

With strength 2, the index treats case as insignificant, so a lookup for Ann@example.com matches a stored ann@example.com. Because the query passes the same collation, the index is used. If the query omitted the collation, the same filter would scan and likely miss the mixed-case row.

Common mistake

Defining the collation on the query but not the index, so the index is unused. Another is inconsistent locales across queries, which produces different matches for the same data and breaks uniqueness expectations.

Verify the behavior

Insert a mixed-case email and confirm a case-insensitive query finds it. Run explain with and without the matching collation and confirm the index is used only when they match. Test the unique index with a case-only variant and confirm it is rejected.

Interview exercise

Why must a query’s collation match the index’s collation?

Answer and reasoning

An index is ordered by a specific comparison rule. If the query asks for a different rule, the index’s order does not correspond to the requested comparison, so the engine cannot use it for matching or sorting and falls back to a collection scan. Matching collations keep the index usable.

Continue learning

Compare index design in Compound indexes and unique indexes. Read the MongoDB collation documentation and try the MongoDB interview questions.

More in MongoDB

read ✓MongoDB · mid

MongoDB Text Search Indexes

Create a weighted text index, query with $text and textScore, and understand the limits of MongoDB's built-in search.

~2 min readread →
read ✓MongoDB · mid

MongoDB Bulk Writes

Batch inserts and updates with bulkWrite, choose ordered or unordered, and handle partial failures correctly.

~2 min readread →
esc