Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js Driver

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.

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.


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.

Running the preceding example, you see the following output:

A document was inserted with the _id: <your _id value>
←  Insert OperationsInsert Multiple Documents →