|
| 1 | +Consider an application that captures user-defined data under the |
| 2 | +``userMetadata`` field and supports querying against that data: |
| 3 | + |
| 4 | +.. code-block:: javascript |
| 5 | + :copyable: false |
| 6 | +
|
| 7 | + { "userMetadata" : { "likes" : [ "dogs", "cats" ] } } |
| 8 | + { "userMetadata" : { "dislikes" : "pickles" } } |
| 9 | + { "userMetadata" : { "age" : 45 } } |
| 10 | + { "userMetadata" : "inactive" } |
| 11 | +
|
| 12 | +Administrators want to create indexes to support queries on any |
| 13 | +subfield of ``userMetadata``. |
| 14 | + |
| 15 | +A wildcard index on ``userMetadata`` |
| 16 | +can support single-field queries on ``userMetadata``, |
| 17 | +``userMetadata.likes``, ``userMetadata.dislikes``, and |
| 18 | +``userMetadata.age``: |
| 19 | + |
| 20 | +.. code-block:: bash |
| 21 | +
|
| 22 | + db.userData.createIndex( { "userMetadata.$**" : 1 } ) |
| 23 | +
|
| 24 | +The index can support the following queries: |
| 25 | +
|
| 26 | +.. code-block:: bash |
| 27 | + :copyable: false |
| 28 | +
|
| 29 | + db.userData.find({ "userMetadata.likes" : "dogs" }) |
| 30 | + db.userData.find({ "userMetadata.dislikes" : "pickles" }) |
| 31 | + db.userData.find({ "userMetadata.age" : { $gt : 30 } }) |
| 32 | + db.userData.find({ "userMetadata" : "inactive" }) |
| 33 | +
|
| 34 | +A non-wildcard index on ``userMetadata`` can only support queries on |
| 35 | +values of ``userMetadata``. |
| 36 | +
|
| 37 | +.. important:: |
| 38 | +
|
| 39 | + Wildcard indexes are not designed to replace workload-based index |
| 40 | + planning. For more information on creating indexes to support |
| 41 | + queries, see :ref:`create-indexes-to-support-queries`. For |
| 42 | + complete documentation on wildcard index limitations, see |
| 43 | + :ref:`wildcard-index-restrictions`. |
| 44 | +
|
| 45 | +
|
| 46 | +The :binary:`~bin.mongod` |
| 47 | +:ref:`featureCompatibilityVersion <view-fcv>` must be ``4.2`` to |
| 48 | +create wildcard indexes. For instructions on setting the fCV, see |
| 49 | +:ref:`Setting the fCV <set-fcv>`. |
0 commit comments