Easy Tutorial
❮ Mongodb Tutorial Mongodb Sort ❯

MongoDB Insert Document

In this section, we will introduce how to insert data into a MongoDB collection.

The document data structure is almost identical to JSON.

All data stored in the collection is in BSON format.

BSON is a binary representation of JSON-like documents, short for Binary JSON.

Insert Document

MongoDB uses the insert() or save() method to insert documents into a collection, with the following syntax:

db.COLLECTION_NAME.insert(document)
or
db.COLLECTION_NAME.save(document)

New methods db.collection.insertOne() and db.collection.insertMany() were added after version 3.2.

db.collection.insertOne() is used to insert a single document into a collection, with the following syntax:

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>
   }
)

db.collection.insertMany() is used to insert multiple documents into a collection, with the following syntax:

db.collection.insertMany(
   [ &lt;document 1>, &lt;document 2>, ... ],
   {
      writeConcern: <document>,
      ordered: <boolean>
   }
)

Parameter descriptions:

Example

The following document can be stored in the col collection of the tutorialpro database in MongoDB:

> db.col.insert({title: 'MongoDB Tutorial', 
    description: 'MongoDB is a NoSQL database',
    by: 'tutorialpro.org',
    url: 'http://www.tutorialpro.org',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

In the above example, col is our collection name. If the collection does not exist in the database, MongoDB will automatically create the collection and insert the document.

To view the inserted document:

> db.col.find()
{ "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB Tutorial", "description" : "MongoDB is a NoSQL database", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>

We can also define the data as a variable, as shown below:

> document=({title: 'MongoDB Tutorial', 
    description: 'MongoDB is a NoSQL database',
    by: 'tutorialpro.org',
    url: 'http://www.tutorialpro.org',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
});

The result after execution is as follows:

{
        "title" : "MongoDB Tutorial",
        "description" : "MongoDB is a NoSQL database",
        "by" : "tutorialpro.org",
        "url" : "http://www.tutorialpro.org",
        "tags" : [
                "mongodb",
```json
{
        "title" : "MongoDB",
        "description" : "MongoDB is a NoSQL database",
        "by" : "Runoob",
        "url" : "http://www.runoob.com",
        "tags" : [
                "database",
                "NoSQL"
        ],
        "likes" : 100
}

Perform the insertion operation:

> db.col.insert(document)
WriteResult({ "nInserted" : 1 })
>

You can also use the db.col.save(document) command to insert a document. If the _id field is not specified, the save() method is similar to the insert() method. If the _id field is specified, it will update the data for that _id.

❮ Mongodb Tutorial Mongodb Sort ❯