title | description | author | ms.author | ms.service | ms.subservice | ms.devlang | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Manage a MongoDB database using JavaScript |
Learn how to manage your Azure Cosmos DB resource when it provides the API for MongoDB with a JavaScript SDK. |
gahl-levy |
gahllevy |
azure-cosmos-db |
mongodb |
javascript |
how-to |
06/23/2022 |
devx-track-js, devguide-js, cosmos-db-dev-journey |
[!INCLUDEMongoDB]
Your MongoDB server in Azure Cosmos DB is available from the common npm packages for MongoDB such as:
Note
The example code snippets are available on GitHub as a JavaScript project.
API for MongoDB reference documentation | MongoDB Package (npm)
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.
Here are some quick rules when naming a database:
Once created, the URI for a database is in this format:
https://<cosmos-account-name>.documents.azure.com/dbs/<database-name>
The database holds the collections and their documents. Use an instance of the Db
class to access the databases on the server.
The following code snippets assume you've already created your client connection and that you close your client connection after these code snippets.
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.
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/200-admin/index.js" id="server_info":::
The preceding code snippet displays the following example console output:
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/200-admin/index.js" id="console_result":::
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:
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/201-does-database-exist/index.js" id="does_database_exist":::
The preceding code snippet displays the following example console output:
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/201-does-database-exist/index.js" id="console_result":::
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.
- MongoClient.Db.Admin.listDatabases
- MongoClient.Db.listCollections
- MongoClient.Db.Collection
- MongoClient.Db.Collection.countDocuments
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/202-get-doc-count/index.js" id="database_object":::
The preceding code snippet displays the following example console output:
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/202-get-doc-count/index.js" id="console_result":::
To get a database object instance, call the following method. This method accepts an optional database name and can be part of a chain.
A database is created when it's 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.
const insertOneResult = await client.db("adventureworks").collection("products").insertOne(doc);
Learn more about working with collections and documents.
A database is removed from the server using the dropDatabase method on the DB class.
:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/300-drop-database/index.js" id="drop_database":::
The preceding code snippet displays the following example console output:
:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/300-drop-database/index.js" id="console_result":::