2

I want to create an index on a nested field in a document in Azure Cosmos DB. E.g. if I have the following schema:

{ 
    'id': 1, 
    'nested': 
        { 
            'mode': 'mode1',
            'text': 'nice text' 
        } 
}

I want to create an index on the field nested.mode. How can this be done?

3
  • 1
    can you explain what you want to achieve? By default, all Cosmos DB is indexed. So the field should have been indexed. Commented Mar 1, 2018 at 13:28
  • A slight subtlety to what @ChunLiu said: with the DocumentDB (SQL) API, all properties are indexed by default. Are you using the DocumentDB API or the MongoDB API? Commented Mar 1, 2018 at 13:50
  • 1
    I am using the MongoDB API of the CosmosDb. If all fields in CosmosDB are already indexed, then if I update a document in the collection, then will it be automatically updated in the indexed storage collections, like the AWS DynamoDb, and hence would it incur extra throughput costs for having indexes? @David Makogon Commented Mar 2, 2018 at 12:34

1 Answer 1

3
Answer recommended by Microsoft Azure Collective

By default, all paths are indexed in Cosmos DB. To index just "nested.mode", you need to specify an indexing policy on the /nested/mode/? path with the appropriate data type/precision. Something like this in JSON within the includedPaths section.

     "path":"/nested/mode/?",
     "indexes":[
        {
           "kind":"Range",
           "dataType":"String",
           "precision":-1
        }

More details here: https://learn.microsoft.com/en-us/azure/cosmos-db/indexing-policies.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks for the answer. If all fields in CosmosDB are already indexed, then if I update a document in the collection, then will it be automatically updated in the indexed storage collections, like the AWS DynamoDb, and hence would it incur extra throughput costs for having indexes on all fields as compared to having indexes on a couple of fields?
yes, but keep in mind that indexing RU costs are very low compared to other databases (including DynamoDB). Each indexed path is only a fractional overhead per document (a few %), not a multiplier for write units.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.