Skip to content

Latest commit

 

History

History
155 lines (105 loc) · 5.62 KB

create-documentdb-nodejs.md

File metadata and controls

155 lines (105 loc) · 5.62 KB
title description services documentationcenter author manager editor ms.assetid ms.service ms.custom ms.workload ms.tgt_pltfrm ms.devlang ms.topic ms.date ms.author
Azure Cosmos DB: Build an app with Node.js and the DocumentDB API | Microsoft Docs
Presents a Node.js code sample you can use to connect to and query the Azure Cosmos DB DocumentDB API
cosmos-db
mimig1
jhubbard
9c0f033c-240e-4fee-8421-08907231087f
cosmos-db
quick start connect, mvc
na
nodejs
quickstart
05/10/2017
mimig

Azure Cosmos DB: Build a DocumentDB API app with Node.js and the Azure portal

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query document, key/value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.

This quick start demonstrates how to create an Azure Cosmos DB account, document database, and collection using the Azure portal. You then build and run a console app built on the DocumentDB Node.js API.

Prerequisites

  • Before you can run this sample, you must have the following prerequisites:

[!INCLUDE quickstarts-free-trial-note] [!INCLUDE cosmos-db-emulator-docdb-api]

Create a database account

[!INCLUDE cosmos-db-create-dbaccount]

Add a collection

[!INCLUDE cosmos-db-create-collection]

Clone the sample application

Now let's clone a DocumentDB API app from github, set the connection string, and run it. You see how easy it is to work with data programmatically.

  1. Open a git terminal window, such as git bash, and CD to a working directory.

  2. Run the following command to clone the sample repository.

    git clone https://github.com/Azure-Samples/azure-cosmos-db-documentdb-nodejs-getting-started.git

Review the code

Let's make a quick review of what's happening in the app. Open the app.js file and you find that these lines of code create the Azure Cosmos DB resources.

  • The documentClient is initialized.

    var client = new documentClient(config.endpoint, { "masterKey": config.primaryKey });
    
  • A new database is created.

    client.createDatabase(config.database, (err, created) => {
        if (err) reject(err)
        else resolve(created);
    });
    
  • A new collection is created.

    client.createCollection(databaseUrl, config.collection, { offerThroughput: 400 }, (err, created) => {
        if (err) reject(err)
        else resolve(created);
    });
    
  • Some documents are created.

    client.createDocument(collectionUrl, document, (err, created) => {
        if (err) reject(err)
        else resolve(created);
    });
    
  • A SQL query over JSON is performed.

    client.queryDocuments(
        collectionUrl,
        'SELECT VALUE r.children FROM root r WHERE r.lastName = "Andersen"'
    ).toArray((err, results) => {
        if (err) reject(err)
        else {
            for (var queryResult of results) {
                let resultString = JSON.stringify(queryResult);
                console.log(`\tQuery returned ${resultString}`);
            }
            console.log();
            resolve(results);
        }
    });
    

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app.

  1. In the Azure portal, in your Azure Cosmos DB account, in the left navigation click Keys, and then click Read-write Keys. You'll use the copy buttons on the right side of the screen to copy the URI and Primary Key into the config.js file in the next step.

    View and copy an access key in the Azure portal, Keys blade

  2. In Open the config.js file.

  3. Copy your URI value from the portal (using the copy button) and make it the value of the endpoint key in config.js.

    config.endpoint = "https://FILLME.documents.azure.com"

  4. Then copy your PRIMARY KEY value from the portal and make it the value of the config.primaryKey in config.js. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.

    config.primaryKey "FILLME"

Run the app

  1. Run npm install in a terminal to install required npm modules

  2. Run node app.js in a terminal to start your node application.

You can now go back to Data Explorer and see query, modify, and work with this new data.

Review SLAs in the Azure portal

[!INCLUDE cosmosdb-tutorial-review-slas]

Clean up resources

If you're not going to continue to use this app, delete all resources created by this quickstart in the Azure portal with the following steps:

  1. From the left-hand menu in the Azure portal, click Resource groups and then click the name of the resource you created.
  2. On your resource group page, click Delete, type the name of the resource to delete in the text box, and then click Delete.

Next steps

In this quickstart, you've learned how to create an Azure Cosmos DB account, create a collection using the Data Explorer, and run an app. You can now import additional data to your Cosmos DB account.

[!div class="nextstepaction"] Import data into Azure Cosmos DB