@@ -183,6 +183,15 @@ the ``sparse`` option.
183183
184184Shard key indexes cannot be partial indexes.
185185
186+ .. _index-partial-equivalent-indexes:
187+
188+ Equivalent Indexes
189+ ~~~~~~~~~~~~~~~~~~
190+
191+ .. include:: /includes/indexes/equivalent-indexes.rst
192+
193+ For an example, see :ref:`index-partial-equivalent-indexes-example`.
194+
186195Examples
187196--------
188197
@@ -296,3 +305,70 @@ greater than or equal to 21.
296305 { username: "amanda" },
297306 { username: "rajiv", age: null }
298307 ] )
308+
309+ .. _index-partial-equivalent-indexes-example:
310+
311+ Equivalent Indexes Example
312+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
313+
314+ .. include:: /includes/indexes/equivalent-indexes.rst
315+
316+ In previous MongoDB versions, you can create two equivalent indexes. The
317+ following example creates a ``pizzas`` collection and two equivalent
318+ indexes named ``index0`` and ``index1``:
319+
320+ .. code-block:: javascript
321+
322+ // Create the pizzas collection
323+ db.pizzas.insertMany( [
324+ { _id: 0, type: "pepperoni", size: "small", price: 4 },
325+ { _id: 1, type: "cheese", size: "medium", price: 7 },
326+ { _id: 2, type: "vegan", size: "large", price: 8 }
327+ ] )
328+
329+ // Create two equivalent indexes with medium pizza sizes
330+ db.pizzas.createIndex(
331+ { type: 1 },
332+ { name: "index0",
333+ partialFilterExpression: { size: "medium" },
334+ collation: { locale: "en_US", strength: 1 }
335+ }
336+ )
337+
338+ db.pizzas.createIndex(
339+ { type: 1 },
340+ { name: "index1",
341+ partialFilterExpression: { size: "MEDIUM" },
342+ collation: { locale: "en_US", strength: 1 }
343+ }
344+ )
345+
346+ The indexes are equivalent because the two indexes specify the same
347+ pizza size and only differ in the text case in the partial filter
348+ expression. Only one index is used by queries: the index that was
349+ created first, which is ``index0`` in the previous example.
350+
351+ Starting in MongoDB 7.3, you cannot create the second index (``index1``)
352+ and this error is returned:
353+
354+ .. code-block:: none
355+ :copyable: false
356+
357+ MongoServerError: Index already exists with a different name: index0
358+
359+ In MongoDB versions earlier than 7.3, you can create the indexes but
360+ only the first index (``index0``) is used with these queries:
361+
362+ .. code-block:: javascript
363+
364+ db.pizzas.find( { type: "cheese", size: "medium" } ).collation(
365+ { locale: "en_US", strength: 1 }
366+ )
367+
368+ db.pizzas.find( { type: "cheese", size: "MEDIUM" } ).collation(
369+ { locale: "en_US", strength: 1 }
370+ )
371+
372+ db.pizzas.find( { type: "cheese", size: "Medium" } ).collation(
373+ { locale: "en_US", strength: 1 }
374+ )
0 commit comments