Join Chander Dhall for an in-depth discussion in this video Exclude indexing at document level, part of Cosmos DB: Import, Manipulate, Index and Query.
- [Instructor] Now we'll talk about indexing. First I would like to add some properties. Most of these properties need to come in from your App.config, but we're just hard-coding these values for now. And then we need the collection ID. In this case, it could be my last name, Dhall. Then we're going to create a function called RunDemo, and it will be an async function, so I'm going to use wait, use control dot, resolve the function, and then make it async, and since it's not going to return anything, it's going to be a type task.
We're going to create a database as we did earlier, client.createDataBasedIfNotExists in async fashion, use new Database Id = the same as the database ID that we just defined, and the next thing would be a collection. I will use UriFactory.CreateDatabaseUri using the database ID comma new DocumentCollection, and all we need is the ID of this collection.
We don't need to add any request options at this moment, we'll just keep it as default. Now, the first thing I want to do is show you what happens if I do not index at the document level. So, DocumentDB or CosmosDB by default indexes every single document. What I want to show you is what happens if we tell CosmosDB to not actually index that document.
We're going to make this function async, we have the database, and we also have the collection. So, the first thing we need to do is create a document. I'm going to do await client.CreateDocumentAsync, and we have collectionUri.
We can get collectionUri using UriFactory.createDocumentCollectionUri, and all it needs is the databaseId and the collectionId. Once we have the collectionUri, the next thing we need is the actual document, and we can create any document we like. I'm just going to add an ID there, which is just 123, and the name is my name, and then company is Cazton, and that seems like a pretty good document.
Next, we could have RequestOptions. There's a typo, we fix the typo, and now we have RequestOptions. We want to make sure that the IndexingDirective equals Exclude. Keep in mind by default it's automatically included, so we have to make sure that we exclude it manually. Now, once we do this, what does this really mean? This means that even though the document will be created, it won't be easily selected on any other property except the ID.
So, let's try this, and say docExists and we want to do a query real quick, CreateDocumentQuery, and then parse in the collectionUri. We got the collectionUri, and then we have our query. We need to select * from root r where r.company= 'Cazton', and whatever value we get, we want to make sure that we can get any value that satisfies this in the entire collection.
And then the next thing would be to make sure that even though the query in line number 56 does not return anything, at least we know that document exists inside Azure CosmosDB instance. So, what we're going to do is run a query client.ReadDocumentAsync, and parse in the SelfLink.
So, query by ID is allowed regardless of being indexed or not because that's the bare minimum. We don't need an index to query by ID. And then lastly, we should be able to delete this document by using the collectionUri. I'm going to add a break point at line number 45, and slowly go over every single line of code to see what happens. As you can see, the collectionUri is dbs/Cazton, which is the name of the database, and then we have colls which stands for collections, and then we have the new name of the new collection which is Dhall.
Next, we want to make sure that this document actually gets created, and it did get created, and you can see that there's metadata about the document, the document size, and the count, the collection size, and all that detail. Next, we are checking if the document exists for the condition, which is r.company='Cazton'. It is a true condition because we do have company, and it is Cazton, however, since this document hasn't been indexed or actually excluded from indexing, this should give us a false.
As you can see, the doc exists as false. We still want to make sure that the document actually exists in the portal. There are two ways we can do it, one is check it right here programmatically, and we see that the document does exist. You can see the details of the document right here. You can see we've got Id, we've got the ResourceId, we also have the SelfLink, and this is the way you can get to the entire document. Another way to do the same would be to go to the portal, and once you're in the portal, you can add this particular query.
Now, if this doesn't show up, this is because the new collection is not showing up. So, what we need to do is refresh, and once we refresh, we should be able to see the new collection. We have the new collection. We can add our query right here and run the query. So, as you can see, if I do search the same thing with ID, it does work.
- Data import scenarios
- Creating a database
- Creating a partitioned collection
- Data manipulation
- Importing documents with a stored procedure
- User-defined functions
- Excluding indexing at a document level
- Range indexing on strings
- Querying with SQL parameters
- Range operations