Book Image

Learning Azure DocumentDB

By : Riccardo Becker
Book Image

Learning Azure DocumentDB

By: Riccardo Becker

Overview of this book

Learning DocumentDB adopts a practical, step-by-step approach to help you learn the basics of DocumentDB and use your new-found abilities in real-life scenarios and enterprise solutions. We start with the absolute basics, such as setting up a DocumentDB environment, and guide you through managing your databases, and executing simple and complex queries. Next, we explain how to work with DocumentDB using the open REST protocol, and demonstrate how JavaScript works with DocumentDB. We’ll also show you how to authenticate and execute queries. Moving on, you’ll find out how to use DocumentDB from within Node.js to kick-start your Node.js projects. Next, you’ll discover how to increase the performance of your DocumentDB database and fine-tune it. Finally, you’ll get to grips with using DocumentDB in conjunction with other services offered from the Microsoft Azure platform.
Table of Contents (15 chapters)
Learning Azure DocumentDB
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating resources


As we have seen before, entities inside DocumentDB are called resources. The way resources are managed is a uniform process, and this makes understanding these CRUD operations easier. This section will discuss how to create resources inside your database.

Creating a collection

Before you can create and update documents, you first need to have a collection available. In Chapter 1, Getting Started with DocumentDB, we saw how a collection can be created straight from the Azure portal. In this section, we are going to create one by using the .NET SDK using Visual Studio 2013 and C#.

The following code snippet shows how you can check if a collection already exists; it also demonstrates how a collection can be created:

//prerequisite before we can work. We need a DocumentClient and a Database.
    DocumentClient client = new DocumentClient(new Uri(docDBUri), key);
    Database database = client.CreateDatabaseQuery().Where(d => d.Id == "devicehub").AsEnumerable().First();

  ...