|
| 1 | +--- |
| 2 | +title: Optimize index creation in Azure Cosmos DB for MongoDB vCore |
| 3 | +titleSuffix: Azure Cosmos DB for MongoDB vCore |
| 4 | +description: Use create Indexing for empty collections in Azure Cosmos DB for MongoDB vCore. |
| 5 | +author: khelanmodi |
| 6 | +ms.author: khelanmodi |
| 7 | +ms.reviewer: sidandrews |
| 8 | +ms.service: cosmos-db |
| 9 | +ms.subservice: mongodb-vcore |
| 10 | +ms.topic: conceptual |
| 11 | +ms.date: 1/24/2024 |
| 12 | +--- |
| 13 | + |
| 14 | +# Optimize index creation in Azure Cosmos DB for MongoDB vCore |
| 15 | + |
| 16 | +[!INCLUDE[MongoDB vCore](../../includes/appliesto-mongodb-vcore.md)] |
| 17 | + |
| 18 | +The `CreateIndexes` Command in Azure Cosmos DB for MongoDB vCore has an option to optimize index creation, especially beneficial for scenarios involving empty collections. This document outlines the usage and expected behavior of this new option. |
| 19 | + |
| 20 | +## Advantages in Specific Scenarios |
| 21 | + |
| 22 | +- **Efficiency in Migration Utilities**: This option is ideal in migration contexts, reducing the time for index creation by preventing delays caused by waiting for transactions with pre-existing snapshots. |
| 23 | +- **Streamlined Index Creation Process**: In Cosmos DB for MongoDB vCore, this translates to a simpler process with a single collection scan, enhancing efficiency. |
| 24 | +- **Enhanced Control**: Users gain more control over the indexing process, crucial in environments balancing read and write operations during index creation. |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +- An existing Azure Cosmos DB for MongoDB vCore cluster. |
| 29 | + - If you don't have an Azure subscription, [create an account for free](https://azure.microsoft.com/free). |
| 30 | + - If you have an existing Azure subscription, [create a new Azure Cosmos DB for MongoDB vCore cluster](quickstart-portal.md). |
| 31 | + |
| 32 | +## Default Setting |
| 33 | + |
| 34 | +The default value of this option is `false`, ensuring backward compatibility and maintaining the existing non-blocking behavior. |
| 35 | + |
| 36 | +## Blocking Option |
| 37 | + |
| 38 | +The `CreateIndexes` Command includes a `{ "blocking": true }` option, designed to provide more control over the indexing process in an empty collection. |
| 39 | + |
| 40 | +Setting `{ "blocking": true }` blocks all write operations (delete, update, insert) to the collection until index creation is completed. This feature is particularly useful in scenarios such as migration utilities where indexes are created on empty collections before data writes commence. |
| 41 | + |
| 42 | +## Create an index using the blocking option |
| 43 | + |
| 44 | +For simplicity, let us consider an example of a blog application with the following setup: |
| 45 | + |
| 46 | +- **Database name**: `cosmicworks` |
| 47 | +- **Collection name**: `products` |
| 48 | + |
| 49 | +To demonstrate the use of this new option in the `cosmicworks` database for an empty collection named `products`. This code snippet demonstrates how to use the blocking option, which will temporarily block write operations to the specified collection during index creation in an empty collection: |
| 50 | + |
| 51 | +```javascript |
| 52 | +use cosmicworks; |
| 53 | +db.runCommand({ |
| 54 | + createIndexes: "products", |
| 55 | + indexes: [{"key":{"name":1}, "name":"name_1"}], |
| 56 | + blocking: true |
| 57 | +}) |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +## Summary |
| 62 | + |
| 63 | +The introduction of the blocking option in the `CreateIndexes` Command of Azure Cosmos DB for MongoDB (vCore) is a strategic enhancement for optimizing index creation for an empty collection. This feature complements the existing non-blocking method, providing an additional tool for scenarios requiring efficient index creation on empty collections. |
| 64 | + |
| 65 | +## Related content |
| 66 | + |
| 67 | +Check out [text indexing](how-to-create-text-index.md), which allows for efficient searching and querying of text-based data. |
| 68 | + |
| 69 | +## Next step |
| 70 | + |
| 71 | +> [!div class="nextstepaction"] |
| 72 | +> [Build a Node.js web application](tutorial-nodejs-web-app.md) |
0 commit comments