Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-48570

Allow wildcard indexes to be compounded with preceding fields

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0-rc0
    • Affects Version/s: None
    • Component/s: Index Maintenance
    • None
    • Query Optimization
    • Fully Compatible
    • QO 2023-04-03

      Wildcard Indexes currently do not support compounding in any fashion. A preceding compound field would support additional use cases.

      Consider a collection containing polymorphic documents that share a common "type" field but may otherwise contain varying content fields...

          "_id": <ObjectId>,
          "type": <int>,
          "content": {
              "field1": <string>,
              "field2": <string>

      In such cases, many queries will want to match documents based on a content field, but only within a given target type. Hence it would be preferable to create the following index:

      db.coll.createIndex({ "type": 1, "content.$**": 1 })

      Which is currently not permitted and will be answered with a generic "wildcard indexes do not allow compounding" error message.

      MongoDB currently scales better with few large collections and few indexes than it does with many small collections and many indexes (WT-5479). Given that one of the major advantages of MongoDB is that it is schema-free, building polymorphic collections seems like the natural way to go. More flexible wildcard indexes could provide key support in this regard.

            ruoxin.xu@mongodb.com Ruoxin Xu
            ralf.strobel Ralf Strobel
            15 Vote for this issue
            27 Start watching this issue