MongoDB stores data in the form of documents, which are JSON-like field and value pairs. MongoDB stores in a format called BSON which is a binary representation of JSON document. MongoDB stores all documents in collections. For an introduction to MongoDB and its set up read here.
In MongoDB, write operations (insert, update and delete) target a single collection. In this article we will see how to insert documents using Mongo shell.
MongoDB provides the following methods for inserting documents into a collection:
db.collection.insert()
– Inserts a single document- db.collection.insertOne()
- db.collection.insertMany()
Insert Single Document using insertOne()
Syntax is:
db.collection.insertOne( <document>, { writeConcern: <document> } )
Write concern describes the level of acknowledgement requested from MongoDB for write operations. By default, write concern is set to w:1
which means an acknowledgement is requested from MongoDB for the write operation propagated to the standalone.
We will insert a new furniture to the “product” collection.
In the script below, we first switch to database “mydb”. Insert a new product and then find it. Before we end the script, we drop the collection.
insertOne.txt:
use mydb db.product.insertOne({product: "Comfi Bunk Bed", finish: "Dark walnut", desc: "Perfect finish", price: 33000}) db.product.find() db.product.drop()
Since the document does not specify an _id field, MongoDB adds the _id field and a generated value to the new document.
Output:
C:\mongodb\sample>mongo < insert.txt 2016-01-15T15:38:14.115+0530 I CONTROL [main]Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.2.0 connecting to: test switched to db mydb { "acknowledged" : true, "insertedId" : ObjectId("5698ce9e58e1ac4477363318") } WriteResult({ "nInserted" : 1 }) { "_id" : ObjectId("5698ce9e58e1ac4477363318"), "product" : "Comfi Bunk Bed", "f inish" : "Dark walnut", "desc" : "Perfect finish", "price" : 33000 } true bye
Insert Multiple Documents Using insertMany()
Using db.collection.insertMany()
, we can inserts multiple documents into a collection.
Syntax is:
db.collection.insertMany( { [ <document 1> , <document 2>, ... ] }, { writeConcern: <document>, ordered: <boolean> } )
where ordered
is an Optional attribute. Based on it MongoDB instance will either perform ordered or unordered insert. Its default value is true.
Let’s insert multiple products.
insertMany.txt:
use mydb db.product.insertMany( [ {product: "Study Table", price: 10000, seller: {name: "Joe Furniture"}}, {product: "Coffee Table", price: 10000, seller: {name: "Billy Furniture"}}, {product: "Study Table", price: 20000, seller: {name: "Sam Furniture"}}, {product: "Study Table", price: 5000, seller: {name: "Sam Furniture"}}, {product: "Wooden Chair", price: 500, seller: {name: "Sam Furniture"}} ]) db.product.find() db.product.drop()
Output:
2016-01-15T17:06:05.903+0530 I CONTROL [main]Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.2.0 connecting to: test switched to db mydb { "acknowledged" : true, "insertedIds" : [ ObjectId("5698d9a5da3a95f9797e37b3"), ObjectId("5698d9a5da3a95f9797e37b4"), ObjectId("5698d9a5da3a95f9797e37b5"), ObjectId("5698d9a5da3a95f9797e37b6"), ObjectId("5698d9a5da3a95f9797e37b7") ] } { "_id" : ObjectId("5698d9a5da3a95f9797e37b3"), "product" : "Study Table", "pri e" : 10000, "seller" : { "name" : "Joe Furniture" } } { "_id" : ObjectId("5698d9a5da3a95f9797e37b4"), "product" : "Coffee Table", "pr ce" : 10000, "seller" : { "name" : "Billy Furniture" } } { "_id" : ObjectId("5698d9a5da3a95f9797e37b5"), "product" : "Study Table", "pri e" : 20000, "seller" : { "name" : "Sam Furniture" } } { "_id" : ObjectId("5698d9a5da3a95f9797e37b6"), "product" : "Study Table", "pri e" : 5000, "seller" : { "name" : "Sam Furniture" } } { "_id" : ObjectId("5698d9a5da3a95f9797e37b7"), "product" : "Wooden Chair", "pr ce" : 500, "seller" : { "name" : "Sam Furniture" } } true bye
By default documents are inserted in order. If application doesn’t depend on ordering of inserts then we can make use of unordered inserts as it will also lead to better performance.
insertManyUnOrdered.txt:
use mydb db.product.insertMany([ {product: "Study Table", price: 10000, seller: {name: "Joe Furniture"}}, {product: "Coffee Table", price: 10000, seller: {name: "Billy Furniture"}}, {product: "Study Table", price: 20000, seller: {name: "Sam Furniture"}}, {product: "Study Table", price: 5000, seller: {name: "Sam Furniture"}}, {product: "Wooden Chair", price: 500, seller: {name: "Sam Furniture"}} ], { ordered: false }) db.product.find() db.product.drop()
Insert Document using insert()
Using db.collection.insert()
, we can insert single as well as multiple documents. In the below example, we insert multiple documents.
insert.txt:
use mydb db.product.insert([{product: "Fabric Sofa", Finish: "Fabric", desc: "Mocha Colored Sofa", price: 40000}, {product: "Leather Sofa", finish: "Italian Leather", desc: "Perfect finish", price: 50000}, {product: "Sofa Bed", finish: "Fabric", desc: "Classic", price: 20000}]) db.product.find() db.product.drop()
Output:
2016-01-15T18:34:06.695+0530 I CONTROL [main]Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.2.0 connecting to: test switched to db mydb BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) { "_id" : ObjectId("5698ee46ca6e64a72733dc47"), "product" : "Fabric Sofa", "Fini sh" : "Fabric", "desc" : "Mocha Colored Sofa", "price" : 40000 } { "_id" : ObjectId("5698ee46ca6e64a72733dc48"), "product" : "Leather Sofa", "fin ish" : "Italian Leather", "desc" : "Perfect finish", "price" : 50000 } { "_id" : ObjectId("5698ee46ca6e64a72733dc49"), "product" : "Sofa Bed", "finish" : "Fabric", "desc" : "Classic", "price" : 20000 } true bye
Insert Embedded Document
We can also embed another document in our product example, like embed sellers information. With the embedded data model, your application can retrieve the complete document information with one query.
insertEmbeddedDoc.txt:
use mydb db.product.insertOne({product: "Elegant Bed", finish: "Teak", desc: "Very elegant", price: 50000, seller: {name: "Joe Furniture"}}) db.product.find() db.product.drop()
Output:
C:\mongodb\sample>mongo < insertEmbeddedDoc.txt 2016-01-15T18:30:00.811+0530 I CONTROL [main]Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.2.0 connecting to: test switched to db mydb { "acknowledged" : true, "insertedId" : ObjectId("5698ed501c6b690e35e73c7b") } { "_id" : ObjectId("5698ed501c6b690e35e73c7b"), "product" : "Elegant Bed", "fini sh" : "Teak", "desc" : "Very elegant", "price" : 50000, "seller" : { "name" : "J oe Furniture" } } true bye
Insert Array
In this example, we insert multiple email IDs as contact information.
insertArray.txt:
use mydb db.product.insertOne({product: "Bed Side Table", finish: "Teak", desc: "Nice and Simple", price: 3000, seller: {name: "Joe Furniture"}, contact: ["[email protected]", "[email protected]"]}) db.product.find() db.product.drop()
Output:
C:\mongodb\sample>mongo < insertArray.txt 2016-01-15T18:27:17.576+0530 I CONTROL [main]Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.2.0 connecting to: test switched to db mydb { "acknowledged" : true, "insertedId" : ObjectId("5698ecad6d60a7f2453d8bc7") } { "_id" : ObjectId("5698ecad6d60a7f2453d8bc7"), "product" : "Bed Side Table", "f inish" : "Teak", "desc" : "Nice and Simple", "price" : 3000, "seller" : { "name" : "Joe Furniture" }, "contact" : [ "[email protected]", "[email protected]" ] } true bye
Download the source code
This was an example about MongoDB, inserting documents using Mongo Shell.