Skip to content

Latest commit

 

History

History
107 lines (63 loc) · 4.66 KB

how-to-javascript-manage-documents.md

File metadata and controls

107 lines (63 loc) · 4.66 KB
title description author ms.author ms.service ms.subservice ms.devlang ms.topic ms.date ms.custom
Create a document in Azure Cosmos DB for MongoDB using JavaScript
Learn how to work with a document in your Azure Cosmos DB for MongoDB database using the JavaScript SDK.
gahl-levy
gahllevy
azure-cosmos-db
mongodb
javascript
how-to
06/23/2022
devx-track-js, devguide-js, cosmos-db-dev-journey

Manage a document in Azure Cosmos DB for MongoDB using JavaScript

[!INCLUDEMongoDB]

Manage your MongoDB documents with the ability to insert, update, and delete documents.

Note

The example code snippets are available on GitHub as a JavaScript project.

API for MongoDB reference documentation | MongoDB Package (npm)

Insert a document

Insert a document, defined with a JSON schema, into your collection.

:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/203-insert-doc/index.js" id="database_object":::

The preceding code snippet displays the following example console output:

:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/203-insert-doc/index.js" id="console_result":::

Document ID

If you don't provide an ID, _id, for your document, one is created for you as a BSON object. The value of the provided ID is accessed with the ObjectId method.

Use the ID to query for documents:

const query = { _id: ObjectId("62b1f43a9446918500c875c5")};

Update a document

To update a document, specify the query used to find the document along with a set of properties of the document that should be updated. You can choose to upsert the document, which inserts the document if it doesn't already exist.

:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/250-upsert-doc/index.js" id="upsert":::

The preceding code snippet displays the following example console output for an insert:

:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/250-upsert-doc/index.js" id="console_result_insert":::

The preceding code snippet displays the following example console output for an update:

:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/250-upsert-doc/index.js" id="console_result_update":::

Bulk updates to a collection

You can perform several operations at once with the bulkWrite operation. Learn more about how to optimize bulk writes for Azure Cosmos DB.

The following bulk operations are available:

:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/251-bulk_write/index.js" id="bulk_write":::

The preceding code snippet displays the following example console output:

:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/251-bulk_write/index.js" id="console_result_bulk_write":::

Delete a document

To delete documents, use a query to define how the documents are found.

:::code language="javascript" source="~/samples-cosmosdb-mongodb-javascript/290-delete-doc/index.js" id="delete":::

The preceding code snippet displays the following example console output:

:::code language="console" source="~/samples-cosmosdb-mongodb-javascript/290-delete-doc/index.js" id="console_result":::

See also