Create Documents

Learn and practice inserting single and multiple documents into a collection.

Insert single record

To create a todo application, we insert a task into the MongoDB collection. We can assign any name to the collection, but it should be relative to the data stored in that collection. Here, we’ll choose tasks.

We can use the below command to choose a database.

use todo;

Below is the syntax to insert a single document.

db.<collection name>.insertOne(Document, Options);

The Document argument is an object that specifies properties in the <key>:<value> pattern.

Below is the property of the Options argument of the insertOne command.

writeConcern

The example below demonstrates how to insert a task into the tasks collection.

Get hands-on with 1400+ tech skills courses.