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 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.
- Before you can run this sample, you must have the following prerequisites:
[!INCLUDE quickstarts-free-trial-note] [!INCLUDE cosmos-db-emulator-docdb-api]
[!INCLUDE cosmos-db-create-dbaccount]
[!INCLUDE cosmos-db-create-collection]
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.
-
Open a git terminal window, such as git bash, and
CD
to a working directory. -
Run the following command to clone the sample repository.
git clone https://github.com/Azure-Samples/azure-cosmos-db-documentdb-nodejs-getting-started.git
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); } });
Now go back to the Azure portal to get your connection string information and copy it into the app.
-
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. -
In Open the
config.js
file. -
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"
-
Then copy your PRIMARY KEY value from the portal and make it the value of the
config.primaryKey
inconfig.js
. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.config.primaryKey "FILLME"
-
Run
npm install
in a terminal to install required npm modules -
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.
[!INCLUDE cosmosdb-tutorial-review-slas]
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:
- From the left-hand menu in the Azure portal, click Resource groups and then click the name of the resource you created.
- On your resource group page, click Delete, type the name of the resource to delete in the text box, and then click Delete.
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