Wildcard indexes in Azure Cosmos DB’s API for MongoDB

Tim Sander

With our latest service update, you can now create wildcard indexes in accounts that use Azure Cosmos DB’s API for MongoDB. Wildcard indexes are a popular feature in MongoDB 4.2 that allow you to index many properties without enumerating each one. Wildcard indexes make it easy to work with unstructured data and can often simplify development.

In Azure Cosmos DB’s API for MongoDB, all documents in a single collection may not have the same properties. Because collections are schemaless, it can be challenging to define separate indexes for every single property that you need to query. Wildcard indexing can offer an easy solution.

Example:

Let’s explore an example use case for wildcard indexing with a fictional company called Contoso Auto. A cargo logistics organization that is collecting vehicle and package telemetry data.

Contoso Auto created a collection called telemetry. This collection stores data for both packages and trucks. Here are some example documents in this collection:

Document 1:

{
    "_id" : "a72j89",
    "truck_attributes" : {
        "products" : [ "computer", "keyboard", "monitor" ],
        "location" : {
            "longitude" : 47.67491,
            "latitude" : -122.12399
        }
    }
}

Document 2:

{
    "_id" : "b77j22",
    "truck_attributes" : {
        "truck_status" : [ "stopped", "low fuel" ],
        "driver_metadata" : {
            "name" : "Luis", "experience" : "1000", "supervisor" : "Chris" }
        }
    }
}

Creating a wildcard index easily indexes all properties within the truck_attributes:

db.coll.createIndex( { "truck_attributes.$**" : 1 } )

Contoso Auto’s developers don’t know all the possible property names that could show up in the truck_attributes. They also know that new properties could be added later. Since they can’t enumerate all the possible properties within truck_attributes, they should index them with a wildcard index.

You can create a wildcard index for any supported index type in Azure Cosmos DB’s API for MongoDB except for compound indexes. Unlike in MongoDB, wildcard indexes in Azure Cosmos DB’s API for MongoDB support 2dsphere geospatial indexes.

 

Indexing all properties:

In many cases, it may make sense to create a wildcard index to automatically index all properties in your collection:

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

If you are just starting out development using Azure Cosmos DB’s API for MongoDB, you can start out by using a wildcard index to automatically index all properties. You can drop the wildcard index and add indexes on specific properties later, once you better understand your data and query requirements. Indexing only the necessary properties will help optimize write performance. In Contoso Auto’s case, they have a write-heavy scenario so it makes sense for them to only use wildcard indexes when they are unable to enumerate all the possible properties to index.

 

Differences from MongoDB:

In Azure Cosmos DB’s API for MongoDB, queries can use wildcard indexes to achieve the same performance that they could achieve with indexes on each individual field.

In MongoDB, wildcard indexes can only support, at most, one field in any given query predicate. Wildcard indexes in Azure Cosmos DB’s API for MongoDB do not have this limitation. There is no limit to the number of fields that wildcard indexes can support in query predicates. Therefore, unlike in MongoDB, you will observe similar query performance indexing all properties with a wildcard index vs defining a separate index on each individual property.

For example, the following query with filters on multiple properties could solely rely on the index in Azure Cosmos DB’s API for MongoDB, even if you simply indexed all properties with a wildcard index:

db.coll.find(
  {
    $and : [
            { "truck_attributes.driver_metadata.name" : "Tim"}, 
            { "truck_attributes.driver_metadata.supervisor" : "Andrew"}
        ]
  }
)

Learn more about wildcard indexes in Azure Cosmos DB’s API for MongoDB.

0 comments

Discussion is closed.

Feedback usabilla icon