Insert a Document
You can insert a document into a collection using the
collection.insertOne() method. To
insert a document, define an object that contains the fields and values that
you want to store. If the specified collection does not exist, the
insertOne()
method creates the collection.
You can specify more query options using the options
parameter.
For more information on the method parameters, see the
insertOne() API documentation.
For more information on this method, see the
insertOne() API documentation.
If the operation successfully inserts a document, it appends an
insertedId
field to the object passed in the method call, and sets the
value of the field to the _id
of the inserted document.
Compatibility
You can use the Node.js driver to connect and use the insertOne()
method for
deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
To learn more about inserting documents in the Atlas UI for deployments hosted in MongoDB Atlas, see Create, View, Update, and Delete Documents.
Example
Note
You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.
1 import { MongoClient } from "mongodb"; 2 3 // Replace the uri string with your MongoDB deployment's connection string. 4 const uri = "<connection string uri>"; 5 6 // Create a new client and connect to MongoDB 7 const client = new MongoClient(uri); 8 9 async function run() { 10 try { 11 // Connect to the "insertDB" database and access its "haiku" collection 12 const database = client.db("insertDB"); 13 const haiku = database.collection("haiku"); 14 15 // Create a document to insert 16 const doc = { 17 title: "Record of a Shriveled Datum", 18 content: "No bytes, no problem. Just insert a document, in MongoDB", 19 } 20 // Insert the defined document into the "haiku" collection 21 const result = await haiku.insertOne(doc); 22 23 // Print the ID of the inserted document 24 console.log(`A document was inserted with the _id: ${result.insertedId}`); 25 } finally { 26 // Close the MongoDB client connection 27 await client.close(); 28 } 29 } 30 // Run the function and handle any errors 31 run().catch(console.dir);
1 import { MongoClient } from "mongodb"; 2 3 // Replace the uri string with your MongoDB deployment's connection string. 4 const uri = "<connection string uri>"; 5 6 const client = new MongoClient(uri); 7 8 interface Haiku { 9 title: string; 10 content: string; 11 } 12 13 async function run() { 14 try { 15 const database = client.db("insertDB"); 16 // Specifying a Schema is optional, but it enables type hints on 17 // finds and inserts 18 const haiku = database.collection<Haiku>("haiku"); 19 const result = await haiku.insertOne({ 20 title: "Record of a Shriveled Datum", 21 content: "No bytes, no problem. Just insert a document, in MongoDB", 22 }); 23 console.log(`A document was inserted with the _id: ${result.insertedId}`); 24 } finally { 25 await client.close(); 26 } 27 } 28 run().catch(console.dir);
Running the preceding example, you see the following output:
A document was inserted with the _id: <your _id value>