Skip to content

Commit

Permalink
Merge pull request #19146 from Azure/bursteg-update-docdb-code-snippets
Browse files Browse the repository at this point in the history
Update documentdb-nodejs-get-started.md
  • Loading branch information
bursteg committed Jan 13, 2016
2 parents f93b436 + e4833cb commit f09b250
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions articles/documentdb/documentdb-nodejs-get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ Let's create a DocumentDB account. If you already have an account you want to us
2. Locate the folder or directory where you'd like to save your Node.js application.
3. Create two empty JavaScript files with the following commands:
- Windows:
* **fsutil file createnew app.js 0**
* **fsutil file createnew config.js 0**
* ```fsutil file createnew app.js 0```
* ```fsutil file createnew config.js 0```
- Linux/OS X:
* **touch app.js**
* **touch config.js**
* ```touch app.js```
* ```touch config.js```
4. Install the documentdb module via npm. Use the following command:
* **npm install documentdb --save**
* ```npm install documentdb --save```

Great! Now that you've finished setting up, let's start writing some code.

##<a id="Config"></a> Step 3: Set your app's configurations

Open *config.js* in your favorite text editor.
Open ```config.js``` in your favorite text editor.

Then, create an empty object titled *config* and set properties *config.endpoint* and *config.authKey* to your DocumentDB endpoint and authorization key. Both these configurations can be found in the [Azure Portal](https://portal.azure.com).
Then, create an empty object titled ```config``` and set properties ```config.endpoint``` and ```config.authKey``` to your DocumentDB endpoint and authorization key. Both these configurations can be found in the [Azure Portal](https://portal.azure.com).

![Screen shot of the Azure Portal, showing a DocumentDB account, with the ACTIVE hub highlighted, the KEYS button highlighted on the DocumentDB account blade, and the URI, PRIMARY KEY and SECONDARY KEY values highlighted on the Keys blade][keys]

Expand All @@ -83,7 +83,7 @@ Then, create an empty object titled *config* and set properties *config.endpoint
config.endpoint = "https://YOUR_ENDPOINT_URI.documents.azure.com:443/";
config.authKey = "oqTveZeWlbtnJQ2yMj23HOAViIr0ya****YOUR_AUTHORIZATION_KEY****ysadfbUV+wUdxwDAZlCfcVzWp0PQg==";

Let's now add the *database id*, *collection id*, and *JSON documents* to your *config* object. Below where you set your *config.endpoint* and *config.authKey* properties, add the following code. If you already have data you'd like to store in your database, you can use DocumentDB's [Data Migration tool](documentdb-import-data.md) rather than adding the document definitions.
Let's now add the ```database id```, ```collection id```, and ```JSON documents``` to your ```config``` object. Below where you set your ```config.endpoint``` and ```config.authKey``` properties, add the following code. If you already have data you'd like to store in your database, you can use DocumentDB's [Data Migration tool](documentdb-import-data.md) rather than adding the document definitions.

config.dbDefinition = {"id": "FamilyRegistry"};
config.collDefinition = {"id": "FamilyCollection"};
Expand Down Expand Up @@ -163,27 +163,27 @@ Let's now add the *database id*, *collection id*, and *JSON documents* to your *

config.docsDefinitions = documents;

The database, collection, and document definitions will act as your DocumentDB *database id*, *collection id*, and documents' data.
The database, collection, and document definitions will act as your DocumentDB ```database id```, ```collection id```, and documents' data.

Finally, export your *config* object, so that you can reference it within the *app.js* file.
Finally, export your ```config``` object, so that you can reference it within the ```app.js``` file.

module.exports = config;

##<a id="Connect"></a> Step 4: Connect to a DocumentDB account

Open your empty *app.js* file in the text editor. Import the *documentdb* module and your newly created *config* module.
Open your empty ```app.js``` file in the text editor. Import the ```documentdb``` module and your newly created ```config``` module.

var documentClient = require("documentdb").DocumentClient;
var config = require("./config");

Next, let's use the previously saved *config.endpoint* and *config.authKey* to create a new DocumentClient.
Next, let's use the previously saved ```config.endpoint``` and ```config.authKey``` to create a new DocumentClient.

var client = new documentClient(config.endpoint, {"masterKey": config.authKey});

Now that you've connected to a DocumentDB account, let's take a look at working with DocumentDB resources.

## Step 5: Create a node database
A [database](documentdb-resources.md#databases) can be created by using the [createDatabase](https://azure.github.io/azure-documentdb-node/DocumentClient.html) function of the **DocumentClient** class. A database is the logical container of document storage partitioned across collections. Add a function for creating your new database in the app.js file with the *id* specified in the *config* object. We'll first check to make sure a database with the same *FamilyRegistry* id does not already exist. If it does exist, we'll return that database instead of creating a new one.
A [database](documentdb-resources.md#databases) can be created by using the [createDatabase](https://azure.github.io/azure-documentdb-node/DocumentClient.html) function of the **DocumentClient** class. A database is the logical container of document storage partitioned across collections. Add a function for creating your new database in the app.js file with the ```id``` specified in the ```config``` object. We'll first check to make sure a database with the same ```FamilyRegistry``` id does not already exist. If it does exist, we'll return that database instead of creating a new one.

var getOrCreateDatabase = function(callback) {
var querySpec = {
Expand Down Expand Up @@ -214,7 +214,7 @@ A [database](documentdb-resources.md#databases) can be created by using the [cre

> [AZURE.WARNING] **CreateDocumentCollectionAsync** will create a new S1 collection, which has pricing implications. For more details, please visit our [pricing page](https://azure.microsoft.com/pricing/details/documentdb/).
A [collection](documentdb-resources.md#collections) can be created by using the [createCollection](https://azure.github.io/azure-documentdb-node/DocumentClient.html) function of the **DocumentClient** class. A collection is a container of JSON documents and associated JavaScript application logic. The newly created collection will be mapped to a [S1 performance level](documentdb-performance-levels.md). Add a function for creating your new collection in the app.js file with the *id* specified in the *config* object. Again, we'll check to make sure a collection with the same *FamilyCollection* id does not already exist. If it does exist, we'll return that collection instead of creating a new one.
A [collection](documentdb-resources.md#collections) can be created by using the [createCollection](https://azure.github.io/azure-documentdb-node/DocumentClient.html) function of the **DocumentClient** class. A collection is a container of JSON documents and associated JavaScript application logic. The newly created collection will be mapped to a [S1 performance level](documentdb-performance-levels.md). Add a function for creating your new collection in the app.js file with the ```id``` specified in the ```config``` object. Again, we'll check to make sure a collection with the same ```FamilyCollection``` id does not already exist. If it does exist, we'll return that collection instead of creating a new one.

var getOrCreateCollection = function(callback) {

Expand Down Expand Up @@ -246,7 +246,7 @@ A [collection](documentdb-resources.md#collections) can be created by using the
##<a id="CreateDoc"></a>Step 7: Create a document
A [document](documentdb-resources.md#documents) can be created by using the [createDocument](https://azure.github.io/azure-documentdb-node/DocumentClient.html) function of the **DocumentClient** class. Documents are user defined (arbitrary) JSON content. You can now insert a document into DocumentDB.

Next, add a function to app.js for creating the documents containing the JSON data saved in the *config* object. Again, we'll check to make sure a document with the same id does not already exist.
Next, add a function to app.js for creating the documents containing the JSON data saved in the ```config``` object. Again, we'll check to make sure a document with the same id does not already exist.

var getOrCreateDocument = function(document, callback) {
var querySpec = {
Expand Down Expand Up @@ -280,7 +280,7 @@ Congratulations! You now have functions for creating a database, collection, and

##<a id="Query"></a>Step 8: Query DocumentDB resources

DocumentDB supports [rich queries](documentdb-sql-query.md) against JSON documents stored in each collection. The following sample code shows a query that you can run against the documents in your collection. Add the following function to the *app.js* file. DocumentDB supports SQL-like queries as shown below. For more information on building complex queries, check out the [Query Playground](https://www.documentdb.com/sql/demo) and the [query documentation](documentdb-sql-query.md).
DocumentDB supports [rich queries](documentdb-sql-query.md) against JSON documents stored in each collection. The following sample code shows a query that you can run against the documents in your collection. Add the following function to the ```app.js``` file. DocumentDB supports SQL-like queries as shown below. For more information on building complex queries, check out the [Query Playground](https://www.documentdb.com/sql/demo) and the [query documentation](documentdb-sql-query.md).

var queryCollection = function(documentId, callback) {
var querySpec = {
Expand Down Expand Up @@ -331,7 +331,7 @@ The order of function calls will be
* *queryCollection*
* *cleanup*

Add the following code snippet to the bottom of your code in *app.js*.
Add the following code snippet to the bottom of your code in ```app.js```.

getOrCreateDatabase(function(err, db) {
if(err) return console.log(err);
Expand Down Expand Up @@ -367,7 +367,7 @@ Add the following code snippet to the bottom of your code in *app.js*.

You are now ready to run your Node.js application!

In your terminal, locate your *app.js* file and run the command: **node app.js**
In your terminal, locate your ```app.js``` file and run the command: ```node app.js```

You should see the output of your get started app. The output should match the example text below.

Expand Down Expand Up @@ -432,9 +432,9 @@ To build the GetStarted solution that contains all the samples in this article,
- The [GetStarted](https://github.com/Azure-Samples/documentdb-node-getting-started) solution available on GitHub.

Install the **documentdb** module via npm. Use the following command:
* **npm install documentdb --save**
* ```npm install documentdb --save```

Next, in the *config.js* file, update the config.endpoint and config.authKey values as described in [Step 3: Set your app's configurations](#Config).
Next, in the ```config.js``` file, update the config.endpoint and config.authKey values as described in [Step 3: Set your app's configurations](#Config).

## Next steps

Expand Down

0 comments on commit f09b250

Please sign in to comment.