Skip to content

Commit b20137e

Browse files
authored
Merge branch 'MicrosoftDocs:main' into main
2 parents d5b4c02 + d6ba8b2 commit b20137e

File tree

72 files changed

+1012
-261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1012
-261
lines changed

articles/cosmos-db/mongodb/TOC.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@
284284
href: connect-using-compass.md
285285
- name: Connect using Robo 3T
286286
href: connect-using-robomongo.md
287+
- name: Develop applications
288+
items:
289+
- name: JavaScript
290+
items:
291+
- name: Get started
292+
href: how-to-javascript-get-started.md
293+
- name: Manage databases
294+
href: how-to-javascript-manage-databases.md
295+
- name: Manage collections
296+
href: how-to-javascript-manage-collections.md
297+
- name: Manage documents
298+
href: how-to-javascript-manage-documents.md
299+
- name: Manage queries
300+
href: how-to-javascript-manage-queries.md
287301
- name: Connect using Mongoose
288302
href: connect-using-mongoose.md
289303
- name: Distribute reads globally
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: Get started with Azure Cosmos DB MongoDB API and JavaScript
3+
description: Get started developing a JavaScript application that works with Azure Cosmos DB MongoDB API. This article helps you learn how to set up a project and configure access to an Azure Cosmos DB MongoDB API database.
4+
author: seesharprun
5+
ms.author: sidandrews
6+
ms.service: cosmos-db
7+
ms.subservice: cosmosdb-mongo
8+
ms.devlang: javascript
9+
ms.topic: how-to
10+
ms.date: 06/23/2022
11+
ms.custom: devx-track-js
12+
13+
---
14+
15+
# Get started with Azure Cosmos DB MongoDB API and JavaScript
16+
[!INCLUDE[appliesto-mongo-api](../includes/appliesto-mongodb-api.md)]
17+
18+
This article shows you how to connect to Azure Cosmos DB MongoDB API using the native MongoDB npm package. Once connected, you can perform operations on databases, collections, and docs.
19+
20+
> [!NOTE]
21+
> The [example code snippets](https://github.com/Azure-Samples/cosmos-db-mongodb-api-javascript-samples) are available on GitHub as a JavaScript project.
22+
23+
[MongoDB API reference documentation](https://docs.mongodb.com/drivers/node) | [MongoDB Package (npm)](https://www.npmjs.com/package/mongodb)
24+
25+
26+
## Prerequisites
27+
28+
* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free).
29+
* [Node.js LTS](https://nodejs.org/en/download/)
30+
* [Azure Command-Line Interface (CLI)](/cli/azure/) or [Azure PowerShell](/powershell/azure/)
31+
* [Azure Cosmos DB MongoDB API resource](quickstart-javascript.md#create-an-azure-cosmos-db-account)
32+
33+
## Create a new JavaScript app
34+
35+
1. Create a new JavaScript application in an empty folder using your preferred terminal. Use the [``npm init``](https://docs.npmjs.com/cli/v8/commands/npm-init) command to begin the prompts to create the `package.json` file. Accept the defaults for the prompts.
36+
37+
```console
38+
npm init
39+
```
40+
41+
2. Add the [MongoDB](https://www.npmjs.com/package/mongodb) npm package to the JavaScript project. Use the [``npm install package``](https://docs.npmjs.com/cli/v8/commands/npm-install) command specifying the name of the npm package. The `dotenv` package is used to read the environment variables from a `.env` file during local development.
42+
43+
```console
44+
npm install mongodb dotenv
45+
```
46+
47+
3. To run the app, use a terminal to navigate to the application directory and run the application.
48+
49+
```console
50+
node index.js
51+
```
52+
53+
## Connect with MongoDB native driver to Azure Cosmos DB MongoDB API
54+
55+
To connect with the MongoDB native driver to Azure Cosmos DB, create an instance of the [``MongoClient``](https://mongodb.github.io/node-mongodb-native/4.5/classes/MongoClient.html#connect) class. This class is the starting point to perform all operations against databases.
56+
57+
The most common constructor for **MongoClient** has two parameters:
58+
59+
| Parameter | Example value | Description |
60+
| --- | --- | --- |
61+
| ``url`` | ``COSMOS_CONNECTION_STRIN`` environment variable | MongoDB API connection string to use for all requests |
62+
| ``options`` | `{ssl: true, tls: true, }` | [MongoDB Options](https://mongodb.github.io/node-mongodb-native/4.5/interfaces/MongoClientOptions.html) for the connection. |
63+
64+
Refer to the [Troubleshooting guide](error-codes-solutions.md) for connection issues.
65+
66+
## Get resource name
67+
68+
### [Azure CLI](#tab/azure-cli)
69+
70+
[!INCLUDE [Azure CLI - get resource name](<./includes/azure-cli-get-resource-name.md>)]
71+
72+
### [PowerShell](#tab/azure-powershell)
73+
74+
[!INCLUDE [Powershell - set resource name](<./includes/powershell-set-resource-name.md>)]
75+
76+
### [Portal](#tab/azure-portal)
77+
78+
Skip this step and use the information for the portal in the next step.
79+
80+
---
81+
82+
## Retrieve your connection string
83+
84+
### [Azure CLI](#tab/azure-cli)
85+
86+
[!INCLUDE [Azure CLI - get connection string](<./includes/azure-cli-get-connection-string.md>)]
87+
88+
### [PowerShell](#tab/azure-powershell)
89+
90+
[!INCLUDE [Powershell - get connection string](<./includes/powershell-get-connection-string.md>)]
91+
92+
### [Portal](#tab/azure-portal)
93+
94+
> [!TIP]
95+
> For this guide, we recommend using the resource group name ``msdocs-cosmos``.
96+
97+
[!INCLUDE [Portal - get connection string](<./includes/portal-get-connection-string-from-sign-in.md>)]
98+
99+
---
100+
101+
## Configure environment variables
102+
103+
[!INCLUDE [Multitab - store connection string in environment variable](<./includes/environment-variables-connection-string.md>)]
104+
105+
## Create MongoClient with connection string
106+
107+
108+
1. Add dependencies to reference the MongoDB and DotEnv npm packages.
109+
110+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/101-client-connection-string/index.js" id="package_dependencies":::
111+
112+
2. Define a new instance of the ``MongoClient,`` class using the constructor, and [``process.env.``](https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_env) to use the connection string.
113+
114+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/101-client-connection-string/index.js" id="client_credentials":::
115+
116+
For more information on different ways to create a ``MongoClient`` instance, see [MongoDB NodeJS Driver Quick Start](https://www.npmjs.com/package/mongodb#quick-start).
117+
118+
## Close the MongoClient connection
119+
120+
When your application is finished with the connection remember to close it. That `.close()` call should be after all database calls are made.
121+
122+
```javascript
123+
client.close()
124+
```
125+
126+
## Use MongoDB client classes with Cosmos DB for MongoDB API
127+
128+
[!INCLUDE [Conceptual object model](<./includes/conceptual-object-model.md>)]
129+
130+
Each type of resource is represented by one or more associated JavaScript classes. Here's a list of the most common classes:
131+
132+
| Class | Description |
133+
|---|---|
134+
|[``MongoClient``](https://mongodb.github.io/node-mongodb-native/4.5/classes/MongoClient.html)|This class provides a client-side logical representation for the MongoDB API layer on Cosmos DB. The client object is used to configure and execute requests against the service.|
135+
|[``Db``](https://mongodb.github.io/node-mongodb-native/4.5/classes/Db.html)|This class is a reference to a database that may, or may not, exist in the service yet. The database is validated server-side when you attempt to access it or perform an operation against it.|
136+
|[``Collection``](https://mongodb.github.io/node-mongodb-native/4.5/classes/Collection.html)|This class is a reference to a collection that also may not exist in the service yet. The collection is validated server-side when you attempt to work with it.|
137+
138+
The following guides show you how to use each of these classes to build your application.
139+
140+
**Guide**:
141+
142+
* [Manage databases](how-to-javascript-manage-databases.md)
143+
* [Manage collections](how-to-javascript-manage-collections.md)
144+
* [Manage documents](how-to-javascript-manage-documents.md)
145+
* [Use queries to find documents](how-to-javascript-manage-queries.md)
146+
147+
## See also
148+
149+
- [Package (NuGet)](https://www.nuget.org/packages/Microsoft.Azure.Cosmos)
150+
- [API reference](https://docs.mongodb.com/drivers/node)
151+
152+
## Next steps
153+
154+
Now that you've connected to a MongoDB API account, use the next guide to create and manage databases.
155+
156+
> [!div class="nextstepaction"]
157+
> [Create a database in Azure Cosmos DB MongoDB API using JavaScript](how-to-javascript-manage-databases.md)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Create a collection in Azure Cosmos DB MongoDB API using JavaScript
3+
description: Learn how to work with a collection in your Azure Cosmos DB MongoDB API database using the JavaScript SDK.
4+
author: seesharprun
5+
ms.author: sidandrews
6+
ms.service: cosmos-db
7+
ms.subservice: cosmosdb-mongo
8+
ms.devlang: javascript
9+
ms.topic: how-to
10+
ms.date: 06/23/2022
11+
ms.custom: devx-track-js
12+
---
13+
14+
# Manage a collection in Azure Cosmos DB MongoDB API using JavaScript
15+
16+
[!INCLUDE[appliesto-mongodb-api](../includes/appliesto-mongodb-api.md)]
17+
18+
Manage your MongoDB collection stored in Cosmos DB with the native MongoDB client driver.
19+
20+
> [!NOTE]
21+
> The [example code snippets](https://github.com/Azure-Samples/cosmos-db-mongodb-api-javascript-samples) are available on GitHub as a JavaScript project.
22+
23+
[MongoDB API reference documentation](https://docs.mongodb.com/drivers/node) | [MongoDB Package (npm)](https://www.npmjs.com/package/mongodb)
24+
25+
26+
## Name a collection
27+
28+
In Azure Cosmos DB, a collection is analogous to a table in a relational database. When you create a collection, the collection name forms a segment of the URI used to access the collection resource and any child docs.
29+
30+
Here are some quick rules when naming a collection:
31+
32+
* Keep collection names between 3 and 63 characters long
33+
* Collection names can only contain lowercase letters, numbers, or the dash (-) character.
34+
* Container names must start with a lowercase letter or number.
35+
36+
## Get collection instance
37+
38+
Use an instance of the **Collection** class to access the collection on the server.
39+
40+
* [MongoClient.Db.Collection](https://mongodb.github.io/node-mongodb-native/4.7/classes/Collection.html)
41+
42+
The following code snippets assume you've already created your [client connection](how-to-javascript-get-started.md#create-mongoclient-with-connection-string) and that you [close your client connection](how-to-javascript-get-started.md#close-the-mongoclient-connection) after these code snippets.
43+
44+
## Create a collection
45+
46+
To create a collection, insert a document into the collection.
47+
48+
* [MongoClient.Db.Collection](https://mongodb.github.io/node-mongodb-native/4.5/classes/Db.html#collection)
49+
* [MongoClient.Db.Collection.insertOne](https://mongodb.github.io/node-mongodb-native/4.7/classes/Collection.html#insertOne)
50+
* [MongoClient.Db.Collection.insertMany](https://mongodb.github.io/node-mongodb-native/4.7/classes/Collection.html#insertMany)
51+
52+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/203-insert-doc/index.js" id="database_object":::
53+
54+
## Drop a collection
55+
56+
* [MongoClient.Db.dropCollection](https://mongodb.github.io/node-mongodb-native/4.7/classes/Db.html#dropCollection)
57+
58+
Drop the collection from the database to remove it permanently. However, the next insert or update operation that accesses the collection will create a new collection with that name.
59+
60+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/299-drop-collection/index.js" id="drop_collection":::
61+
62+
The preceding code snippet displays the following example console output:
63+
64+
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/299-drop-collection/index.js" id="console_result":::
65+
66+
## Get collection indexes
67+
68+
An index is used by the MongoDB query engine to improve performance to database queries.
69+
70+
* [MongoClient.Db.Collection.indexes](https://mongodb.github.io/node-mongodb-native/4.7/classes/Collection.html#indexes)
71+
72+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/225-get-collection-indexes/index.js" id="collection":::
73+
74+
The preceding code snippet displays the following example console output:
75+
76+
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/225-get-collection-indexes/index.js" id="console_result":::
77+
78+
## See also
79+
80+
- [Get started with Azure Cosmos DB MongoDB API and JavaScript](how-to-javascript-get-started.md)
81+
- [Create a database](how-to-javascript-manage-databases.md)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: Manage a MongoDB database using JavaScript
3+
description: Learn how to manage your Cosmos DB resource when it provides the MongoDB API with a JavaScript SDK.
4+
author: seesharprun
5+
ms.author: sidandrews
6+
ms.service: cosmos-db
7+
ms.subservice: cosmosdb-mongo
8+
ms.devlang: javascript
9+
ms.topic: how-to
10+
ms.date: 06/23/2022
11+
ms.custom: devx-track-js
12+
---
13+
14+
# Manage a MongoDB database using JavaScript
15+
16+
[!INCLUDE[appliesto-mongodb-api](../includes/appliesto-mongodb-api.md)]
17+
18+
Your MongoDB server in Azure Cosmos DB is available from the common npm packages for MongoDB such as:
19+
20+
* [MongoDB](https://www.npmjs.com/package/mongodb)
21+
22+
> [!NOTE]
23+
> The [example code snippets](https://github.com/Azure-Samples/cosmos-db-mongodb-api-javascript-samples) are available on GitHub as a JavaScript project.
24+
25+
[MongoDB API reference documentation](https://docs.mongodb.com/drivers/node) | [MongoDB Package (npm)](https://www.npmjs.com/package/mongodb)
26+
27+
## Name a database
28+
29+
In Azure Cosmos DB, a database is analogous to a namespace. When you create a database, the database name forms a segment of the URI used to access the database resource and any child resources.
30+
31+
Here are some quick rules when naming a database:
32+
33+
* Keep database names between 3 and 63 characters long
34+
* Database names can only contain lowercase letters, numbers, or the dash (-) character.
35+
* Database names must start with a lowercase letter or number.
36+
37+
Once created, the URI for a database is in this format:
38+
39+
``https://<cosmos-account-name>.documents.azure.com/dbs/<database-name>``
40+
41+
## Get database instance
42+
43+
The database holds the collections and their documents. Use an instance of the **Db** class to access the databases on the server.
44+
45+
* [MongoClient.Db](https://mongodb.github.io/node-mongodb-native/4.7/classes/Db.html)
46+
47+
The following code snippets assume you've already created your [client connection](how-to-javascript-get-started.md#create-mongoclient-with-connection-string) and that you [close your client connection](how-to-javascript-get-started.md#close-the-mongoclient-connection) after these code snippets.
48+
49+
## Get server information
50+
51+
Access the **Admin** class to retrieve server information. You don't need to specify the database name in the `db` method. The information returned is specific to MongoDB and doesn't represent the Azure Cosmos DB platform itself.
52+
53+
* [MongoClient.Db.Admin](https://mongodb.github.io/node-mongodb-native/4.7/classes/Admin.html)
54+
55+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/200-admin/index.js" id="server_info":::
56+
57+
The preceding code snippet displays the following example console output:
58+
59+
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/200-admin/index.js" id="console_result":::
60+
61+
## Does database exist?
62+
63+
The native MongoDB driver for JavaScript creates the database if it doesn't exist when you access it. If you would prefer to know if the database already exists before using it, get the list of current databases and filter for the name:
64+
65+
* [MongoClient.Db.Admin.listDatabases](https://mongodb.github.io/node-mongodb-native/4.7/classes/Db.html)
66+
67+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/201-does-database-exist/index.js" id="does_database_exist":::
68+
69+
The preceding code snippet displays the following example console output:
70+
71+
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/201-does-database-exist/index.js" id="console_result":::
72+
73+
## Get list of databases, collections, and document count
74+
75+
When you manage your MongoDB server programmatically, it's helpful to know what databases and collections are on the server and how many documents in each collection.
76+
77+
* [MongoClient.Db.Admin.listDatabases](https://mongodb.github.io/node-mongodb-native/4.7/classes/Db.html)
78+
* [MongoClient.Db.listCollections](https://mongodb.github.io/node-mongodb-native/4.7/classes/Db.html#listCollections)
79+
* [MongoClient.Db.Collection](https://mongodb.github.io/node-mongodb-native/4.7/classes/Collection.html)
80+
* [MongoClient.Db.Collection.countDocuments](https://mongodb.github.io/node-mongodb-native/4.7/classes/Collection.html#countDocuments)
81+
82+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/202-get-doc-count/index.js" id="database_object":::
83+
84+
The preceding code snippet displays the following example console output:
85+
86+
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/202-get-doc-count/index.js" id="console_result":::
87+
88+
## Get database object instance
89+
90+
To get a database object instance, call the following method. This method accepts an optional database name and can be part of a chain.
91+
92+
* [``MongoClient.Db``](https://mongodb.github.io/node-mongodb-native/4.5/classes/Db.html)
93+
94+
A database is created when it is accessed. The most common way to access a new database is to add a document to a collection. In one line of code using chained objects, the database, collection, and doc are created.
95+
96+
```javascript
97+
const insertOneResult = await client.db("adventureworks").collection("products").insertOne(doc);
98+
```
99+
100+
Learn more about working with [collections](how-to-javascript-manage-collections.md) and documents.
101+
102+
## Drop a database
103+
104+
A database is removed from the server using the dropDatabase method on the DB class.
105+
106+
* [DB.dropDatabase](https://mongodb.github.io/node-mongodb-native/4.7/classes/Db.html#dropDatabase)
107+
108+
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/300-drop-database/index.js" id="drop_database":::
109+
110+
The preceding code snippet displays the following example console output:
111+
112+
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/300-drop-database/index.js" id="console_result":::
113+
114+
## See also
115+
116+
- [Get started with Azure Cosmos DB MongoDB API and JavaScript](how-to-javascript-get-started.md)
117+
- Work with a collection](how-to-javascript-manage-collections.md)

0 commit comments

Comments
 (0)