Join Chander Dhall for an in-depth discussion in this video Multiple ways to select all, part of Cosmos DB: Import, Manipulate, Index and Query.
- [Instructor] Now that we've looked at different indexing options, we'll go and have some querying fun. We don't need a lot of this data, so I'm going to get rid of this. And we don't need anything to do with indexing, at least as of now. So, we'll get rid of this code too. We'll go ahead and change the collection to Company, cause that's a type collection.
We already have a clause that Company dot CS that we can work with. We need to remove this code, because we don't want to delete our collection Async. Or, maybe just move it towards the bottom. Since it already has this, we don't need that code. So, the first thing would be to do a very simply query. So for example, our companies equals from company in client dot create document query, as we've been doing.
The only difference now is that we're going to do it in a type fashion, and say we want the result back. Except, it has to be of the type company. And then we can add default options. And in this case, the most important option is Enable Cross Query equals true. And when I say Cross Query, I mean the Cross Partition Query. Now we're going to foreach the results. It could be companies dot To List. That's one way to do it, or we could have done the To List before we got here.
And then, change this to company. Either way, this should work. Console dot Write Line, and then add a company. Now, company in itself is going to be quite big, so what I'll do is just say company dot name. And then, just a typo here. We forgot to select company. By the way, company is just a variable. I could have called it X Y Z, it doesn't really matter at the end of the day.
And, maybe we get rid of this for now, because we don't want to delete and recreate the collection, cause it already has a lot of the data we created previously in the course. And just making sure that we have dot Name, because we don't want an entire object here. And keep in mind, even when you're doing demos, you are paying for the request units. Let's do control F five. You can see we got a lot of results back. It's a lot of companies. And pretty much, we went for select star, so we have all the companies that are in the Azure Cosmos DB instance.
Now, this is one way to do this query. We could do this query in different ways. So the first one was more like a link query. Now, can we use Lambdas to do the same thing? Of course. So what we could do is Create Document Query. Say collection URI, and then we can say, new feed options. And in this case, we have the same exact option which is true.
Control F five. See, we've got the same exact result back. And a third way of doing the same thing would be, let's comment this code for now. And we've been doing this throughout the course. You can see, we have Create Document Query. And of course, of type Company, but we're going to use just the regular stream query with Select star from root R with the default options.
And as you can see, that we're repeating this code over and over again. So what we could do, is just copy this, and create a field here that gets us this value. So we could say, Default Options, and now, push it right here. Control F five.
As you can see, we get the same result back. So, three different ways to do the same exact thing. This is just when you query all the documents. Of course, as you know, that querying all the documents is usually not a very good idea, but I just wanted to show you that this is how you could do it in three different ways.
- 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