Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Are you looking to migrate an existing MongoDB application or use MongoDB Query Language (MQL) features? Consider Azure DocumentDB.
Are you looking for a database solution for high-scale scenarios with a 99.999% availability service level agreement (SLA), instant autoscale, and automatic failover across multiple regions? Consider Azure Cosmos DB for NoSQL.
One of the most basic operations is inserting data into a collection. This article covers how to insert data by using the Mongo Shell (Mongosh).
Insert a single document
The most basic way to insert data into MongoDB is to insert a single document. To insert data in this way, you can use the db.collection.insertOne() method. The insertOne() method takes a single document as its argument and inserts it into the specified collection. Here's an example of how you might use this method:
db.myCollection.insertOne({
name: "John Smith",
age: 30,
address: "123 Main St"
});
This example inserts a document into the myCollection collection. It has the following fields: name, age, and address. After you run the command, you see acknowledged: true and insertedId: ObjectId("5f5d5f5f5f5f5f5f5f5f5f") in the output, where the insertedId is the unique identifier generated by MongoDB for the inserted document.
Insert multiple documents
In many cases, you need to insert multiple documents at once. To insert multiple documents, you can use the db.collection.insertMany() method. The insertMany() method takes an array of documents as its argument and inserts them into the specified collection. Here's an example:
db.myCollection.insertMany([
{name: "Jane Doe", age: 25, address: "456 Park Ave"},
{name: "Bob Smith", age: 35, address: "789 Elm St"},
{name: "Sally Johnson", age: 40, address: "111 Oak St"}
]);
This example inserts three documents into the myCollection collection. Each document has the same fields as the previous example: name, age, and address. The insertMany() method returns acknowledged: true and insertedIds: [ObjectId("5f5d5f5f5f5f5f5f5f5f5f"), ObjectId("5f5d5f5f5f5f5f5f5f5f5f"), ObjectId("5f5d5f5f5f5f5f5f5f5f5f")] where insertedIds is an array of unique identifiers generated by MongoDB for each inserted document.
Insert with options
Both insertOne() and insertMany() accept an optional second argument, which you can use to specify options for the insert operation. For example, to set the ordered option to false, use the following code:
db.myCollection.insertMany([
{name: "Jane Doe", age: 25, address: "456 Park Ave"},
{name: "Bob Smith", age: 35, address: "789 Elm St"},
{name: "Sally Johnson", age: 40, address: "111 Oak St"}
], {ordered: false});
This option tells MongoDB to insert the documents in an unordered fashion. If one document fails to insert, MongoDB continues with the next one. This option improves write performance in Azure Cosmos DB for MongoDB.
Next steps
- Learn how to use Studio 3T with Azure Cosmos DB for MongoDB.
- Learn how to use Robo 3T with Azure Cosmos DB for MongoDB.
- Explore MongoDB samples with Azure Cosmos DB for MongoDB.
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vCores and servers in your existing database cluster, read about estimating request units using vCores or vCPUs.
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner.