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 |
[!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, 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":::
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")};
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":::
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:
-
MongoClient.Db.Collection.bulkWrite
-
insertOne
-
updateOne
-
updateMany
-
deleteOne
-
deleteMany
-
:::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":::
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":::