Join Chander Dhall for an in-depth discussion in this video Create partitioned collection, part of Cosmos DB: Import, Manipulate, Index and Query.
- [Instructor] Since we've already created the database, we will not need line number 19, so feel free to delete it or comment it. Next, what we'll do is create a partition collection, and it needs to be async, simply because it's another out of process call going all the way to document db. And it will return a task of document collection, again do ctrl+. and you can see its Microsoft Azure documents, and then say create partition collection.
In order to create a partition collection, we would need the database ID, we'd also need the collection ID, and then the last thing we'd need is a partition key. Now what's a partition key? This is something that completely depends on your business, your entity, that you're trying to use. In this case, we'll just give it a random example with document collection, and we can just call it test for now. So we have a new instance of document collection, and then we'll add the properties.
So for example, collection.Id will be the collection ID, and that's just passed into this function. And then we can add a partition key path to it. So we go to collection.PartitionKey.paths and then add the partition key. Now this would actually be the actual path to that particular partition key. And we can do a lot of things here. We can also add the nesting policies, and other things, however, we'll show all that in a later example.
Just to keep things simple, you can say document collection and I'm going to call it company collection and again await client.createdocumentcollection if it does not exist. And it texts in few different parameters, as you can see there's a database link, then there's a document collection, and it also has request options. How do I get the link? Well, same thing as we did in the previous course, UriFactory.createDatabaseUri and it gives me the database ID, that's number one.
Number two is collection, and we already have it, and number three is request options. And this can vary, you can do a lot of things here. You can even add the throughput, which in this case, let's say it's 10,000, which is also the same value by default. And say return company collection. Now we need to parse in the names. And we can do that here. We can say create partition collection, and the name of the database, as we created, it was Cazton, the second thing is a collection ID, I can call it company, and then the next thing I could do is the partition key, and this, as you can see, is a path.
Now what does this really mean? It means that we have company, which is our collection, and then we have category code, which is at the same root level as the root object. So if you had a json object, and let's just show you that in a second. But first we need to do wait just to make sure that we are calling the async function. So let's assume we had a json object, and the object looked more like this, it had some kind of ID, and again the ID would be a grade, but I'm just going to say 123 in this case.
And the second thing was category code. And that could be anything, let's say AX or AS. And then you can also have a bigger object, or a nested object as part of the same json. For example, address and the address can have city, it can have other things. Well, as you can see since category code is at the root level, all I have to do is say /category_code. Now assuming we had a bigger object, for example, an address, and that address had other properties in there.
So one could be city, and let's say the city is Los Angeles, well in this case, I would have had to say something like, address/city. Now this is really a path for document db to know where to look for that particular property. But in this case, let's just stick with what we had, which was category code and leave it at the root level. Once we're done, we need to do ctrl+F5.
Once you see press any key to continue, it means we have a collection with the name company, and a partition which is category code. And then you can come back to Azure Portal and hit browse and you can see we have Cazton, and then we also have collection, which has a throughput of 10,000, and it's part of the database.
- 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