{"id":1403,"date":"2020-06-24T09:00:52","date_gmt":"2020-06-24T16:00:52","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=1403"},"modified":"2020-06-19T17:52:00","modified_gmt":"2020-06-20T00:52:00","slug":"mongodb-wildcard-indexes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/mongodb-wildcard-indexes\/","title":{"rendered":"Wildcard indexes in Azure Cosmos DB\u2019s API for MongoDB"},"content":{"rendered":"<p>With our latest service update, you can now create wildcard indexes in accounts that use Azure Cosmos DB\u2019s 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.<\/p>\n<p>In Azure Cosmos DB\u2019s API for MongoDB, all documents in a single collection may not have the same properties. Because collections are <em>schemaless<\/em>, it can be challenging to define separate indexes for every single property that you need to query. Wildcard indexing can offer an easy solution.<\/p>\n<h2>Example:<\/h2>\n<p>Let\u2019s 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.<\/p>\n<p>Contoso Auto created a collection called <strong>telemetry.<\/strong> This collection stores data for both packages and trucks. Here are some example documents in this collection:<\/p>\n<h4><strong>Document 1:<\/strong><\/h4>\n<pre>{\r\n    \"_id\" : \"a72j89\",\r\n    \"truck_attributes\" : {\r\n        \"products\" : [ \"computer\", \"keyboard\", \"monitor\" ],\r\n        \"location\" : {\r\n            \"longitude\" : 47.67491,\r\n            \"latitude\" : -122.12399\r\n        }\r\n    }\r\n}<\/pre>\n<h4><strong>Document 2:<\/strong><\/h4>\n<pre>{\r\n    \"_id\" : \"b77j22\",\r\n    \"truck_attributes\" : {\r\n        \"truck_status\" : [ \"stopped\", \"low fuel\" ],\r\n        \"driver_metadata\" : {\r\n            \"name\" : \"Luis\", \"experience\" : \"1000\", \"supervisor\" : \"Chris\" }\r\n        }\r\n    }\r\n}<\/pre>\n<p>Creating a wildcard index easily indexes all properties within the <strong>truck_attributes:<\/strong><\/p>\n<pre>db.coll.createIndex( { \"truck_attributes.$**\" : 1 } )<\/pre>\n<p>Contoso Auto\u2019s developers don\u2019t know all the possible property names that could show up in the <strong>truck_attributes. <\/strong>They also know that new properties could be added later. Since they can\u2019t enumerate all the possible properties within <strong>truck_attributes<\/strong>, they should index them with a wildcard index.<\/p>\n<p>You can create a wildcard index for any supported index type in Azure Cosmos DB\u2019s API for MongoDB except for compound indexes. <strong>Unlike in MongoDB<\/strong>, wildcard indexes in Azure Cosmos DB\u2019s API for MongoDB support 2dsphere geospatial indexes.<\/p>\n<p>&nbsp;<\/p>\n<h2>Indexing all properties:<\/h2>\n<p>In many cases, it may make sense to create a wildcard index to automatically index all properties in your collection:<\/p>\n<pre>db.coll.createIndex( {\"$**\": 1 } )<\/pre>\n<p>If you are just starting out development using Azure Cosmos DB\u2019s 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\u2019s 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.<\/p>\n<p>&nbsp;<\/p>\n<h2>Differences from MongoDB:<\/h2>\n<p>In Azure Cosmos DB\u2019s API for MongoDB, queries can use wildcard indexes to achieve the same performance that they could achieve with indexes on each individual field.<\/p>\n<p>In MongoDB, wildcard indexes can only support, at most, one field in any given query predicate. Wildcard indexes in Azure Cosmos DB\u2019s API for MongoDB do not have this limitation. There is <strong>no limit<\/strong> to the number of fields that wildcard indexes can support in query predicates. Therefore, <strong>unlike in MongoDB<\/strong>, you <strong>will<\/strong> observe similar query performance indexing all properties with a wildcard index vs defining a separate index on each individual property.<\/p>\n<p>For example, the following query with filters on multiple properties could solely rely on the index in Azure Cosmos DB\u2019s API for MongoDB, even if you simply indexed all properties with a wildcard index:<\/p>\n<pre>db.coll.find(\r\n  {\r\n    $and : [\r\n            { \"truck_attributes.driver_metadata.name\" : \"Tim\"}, \r\n            { \"truck_attributes.driver_metadata.supervisor\" : \"Andrew\"}\r\n        ]\r\n  }\r\n)<\/pre>\n<p>Learn more about <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb-indexing\" target=\"_blank\" rel=\"noopener noreferrer\">wildcard indexes in Azure Cosmos DB\u2019s API for MongoDB.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With our latest service update, you can now create wildcard indexes in accounts that use Azure Cosmos DB\u2019s API for MongoDB. Wildcard indexes are a popular feature in MongoDB 4.2 that allow you to index many properties without enumerating each one.<\/p>\n","protected":false},"author":12128,"featured_media":1466,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[12,15,818],"tags":[],"class_list":["post-1403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcements","category-mongodb-api","category-query"],"acf":[],"blog_post_summary":"<p>With our latest service update, you can now create wildcard indexes in accounts that use Azure Cosmos DB\u2019s API for MongoDB. Wildcard indexes are a popular feature in MongoDB 4.2 that allow you to index many properties without enumerating each one.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/1403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/12128"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=1403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/1403\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/1466"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=1403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=1403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=1403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}