Join Chander Dhall for an in-depth discussion in this video Query, part of NoSQL Development with DocumentDB in Azure.
- [Instructor] Next we'll look at querying the data we just added. Now we can do it in a lot of different ways, but I just want to show you how the query works in node.js. So I'm going to call this query, and then, I could technically take this right here and make sure I see it. This is really our connection URL that we created earlier. And querying is as simple as client.queryDocuments and really needs a collection URL, and then your actual query.
So in this case, we could say something like select start from root R. Ideally I would not do this query because that can bring in thousands of records from the document DB but in this case we just have 10 records, so we should be completely okay. So once you have this, I'm going to convert the query over to an array, and then what if I have an error? So same thing, we're going to create an error or results. Again, this is our callback, so if I have an error, then it will let me know.
In this case, we'll just do a console log. Again, same way as we did it earlier. We're going to go ahead and create a JSON.stringify, and we have the error objective of right here. And if the error doesn't happen, that means the query was successful. So in that case, I'm going to loop to the results of the query. So I could say let the result of results, and really when I'm looping through this, I want to really get it back in some kind of an object that I can display.
And I could just do that in my console.log. In this case, I can say value, and again, it's better to do JSON.stringify of result. And that's pretty much it. So our query is done. Taking care of some semicolons, and there we're going to call this function, which is really query, and we can do another iffy here. So the query function calls itself, add slash N.
So we have a little better formatting than what we were expecting. Clear screen. Node server. And you can see that we have the value. In this case the ID is ten, the ID is seven, the ID is five, six, and we have all the 10 records as we would usually have. So that's how you can actually query your database. You might want to try some of the queries we did earlier in the course, and see what kind of results you get.
- Back-end scaling fundamentals
- Fallacies of distributed computing
- Consistency models
- Data migration
- DocumentDB with .NET and Node.js