Insert Documents into MongoDB
MongoDB is a major NoSQL, open-source, cross-platform document-oriented database. It’s optimised for performance, availability, and scalability. Because MongoDB has a flexible schema, documents in the same collection can have multiple fields or structures.
Data Storage in MongoDB: JSON-style Documents
Fundamentally, documents which are basically JSON-style objects are used by MongoDB to store data. Contained documents represent a single system item or record. In this document-oriented architecture, hierarchical data structures can be created without multi-table joins.
MongoDB internally employs BSON, a binary serialised JSON format. BSON encodes and decodes quickly and supports more data types than JSON.
Basic Document Structure
Documents are collections of key-value pairs. Arrays, timestamps, binary data, integers, booleans, doubles, strings, and other embedded documents are examples of BSON data types that can be used as values. Keys are strings. Because of this dynamic design, documents within the same collection do not require the same fields or structures. Although a schema is not enforced by collections, the documents in a collection usually serve a same or related purpose.
Every MongoDB document needs a primary key in the _id column. In MongoDB, a document’s ObjectId is assigned automatically if you don’t specify one. A 12-byte hexadecimal ObjectId ensures document uniqueness. The first 4 bytes are the timestamp, followed by the machine ID, MongoDB server process ID, and a short incremental value.
Inserting a Single Document
To add a document to MongoDB, use insert() or insertOne(). MongoDB 3.2.x introduces the insertOne() function to clarify single-document insertions, making it the recommended approach.
If the collection does not exist, MongoDB will construct it before inserting the document. Dynamic creation eliminates the requirement for schemas and collection creation instructions, simplifying development.
Here’s an example of inserting a single document using the insert() method in the mongo shell:
> db.mycol.insert({
title: 'MongoDB Overview',
description: 'MongoDB is a NoSQL database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Insertion usually returns a WriteResult object with the number of documents inserted. MongoDB produces an ObjectId for documents without _id fields.
Inserting Multiple Documents
At MongoDB, “batch inserts” allow several documents to be inserted at once. This method decreases network roundtrips and processing overhead for each document, improving performance.
Batch inserts are possible by supplying an array of documents to insert(). In MongoDB 3.2.x and later, insertMany() is the dedicated function.
Here’s an example of inserting multiple documents using db.collection.insert() with an array:
> db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is a NoSQL database',
by: 'tutorials point'
},
{
title: 'NoSQL Overview',
description: 'NoSQL database is very popular',
by: 'tutorials point'
},
{
title: 'Couchbase Overview',
description: 'Couchbase is a NoSQL database',
by: 'tutorials point'
}
])
With insertMany(), the syntax is similar:
> db.movies.insertMany([
{"title" : "Ghostbusters"},
{"title" : "E.T."},
{"title" : "Blade Runner"}
]);
Both “ordered” and “unordered” processes are options when doing bulk inserts.
- Ordered Inserts: If left unspecified, ordered inserts will act in this manner by default. A series of operations are carried out. If there is a problem (for example, a duplicate _id), MongoDB will terminate processing the remaining documents in the list.
- Unordered Inserts: MongoDB has parallel execution capabilities. In the event of an error, the remaining papers in the list will still be processed.
For example, to specify unordered inserts:
> db.movies.insertMany([
{"_id" : 3, "title" : "Sixteen Candles"},
{"_id" : 4, "title" : "The Terminator"},
{"_id" : 4, "title" : "The Princess Bride"}, // This will cause an error
{"_id" : 5, "title" : "Scarface"}
], {"ordered" : false})
In this unordered example, the documents for “Sixteen Candles,” “The Terminator,” and “Scarface” will still be inserted even though “The Princess Bride” has a duplicate _id problem.
Individual BSON documents have a 16 megabyte maximum document size limit. Many drivers automatically divide a batch insert into smaller, conforming portions if it tries to insert more than this limit in a single message. The GridFS API is offered by MongoDB for files larger than 16MB.