MongoDB
In this MongoDB tutorial we will learn to insert documents.
To insert one document into a collection we use the db.collection.insertOne() method.
db.collection.insertOne()
Note! collection is the name of the collection in which you want to insert the document.
collection
In the following example we are inserting one document into students collection.
> db.students.insertOne({ "firstname": "Yusuf", "lastname": "Shakeel", "studentid": "s01" });
On success we will get a similar response in the terminal.
{ "acknowledged" : true, "insertedId" : ObjectId("5d16c693e8cb73839ac9f2ec") }
Note! insertedId is a unique id (primary key) given to each document in a collection by MongoDB.
To fetch the inserted documents in a collection we use the find() method.
find()
> db.students.find() { "_id" : ObjectId("5d16c693e8cb73839ac9f2ec"), "firstname" : "Yusuf", "lastname" : "Shakeel", "studentid" : "s01" }
We will learn more about find() in the Query Document tutorial.
To insert multiple documents we use the db.collection.insertMany() method.
db.collection.insertMany()
In the following example we are inserting multiple documents in the students collection.
> db.students.insertMany([ { "firstname": "Jane", "lastname": "Doe", "studentid": "s02" }, { "firstname": "John", "lastname": "Doe", "studentid": "s03" } ]);
On success we will get the following response.
{ "acknowledged" : true, "insertedIds" : [ ObjectId("5d16c6e5e8cb73839ac9f2ed"), ObjectId("5d16c6e5e8cb73839ac9f2ee") ] }
If we now run the find() method we will get the following output.
> db.students.find() { "_id" : ObjectId("5d16c693e8cb73839ac9f2ec"), "firstname" : "Yusuf", "lastname" : "Shakeel", "studentid" : "s01" } { "_id" : ObjectId("5d16c6e5e8cb73839ac9f2ed"), "firstname" : "Jane", "lastname" : "Doe", "studentid" : "s02" } { "_id" : ObjectId("5d16c6e5e8cb73839ac9f2ee"), "firstname" : "John", "lastname" : "Doe", "studentid" : "s03" }
In MongoDB we can insert documents with varying number of fields.
In the following example we are inserting two new documents in the students collection. The first document has score field. The second document has date_of_birth field.
score
date_of_birth
> db.students.insertMany([ { "firstname": "Alice", "lastname": "Doe", "studentid": "s04", "score": 10.5 }, { "firstname": "Bob", "lastname": "Doe", "studentid": "s05", "date_of_birth": new Date("2000-01-01") } ]);
The date_of_birth is in the new Date("<YYYY-mm-dd>") format and it returns the ISODate with the specified date.
new Date("<YYYY-mm-dd>")
If we now fetch the documents using find().pretty() method we will get the following output.
find().pretty()
> db.students.find().pretty() { "_id" : ObjectId("5d16c9e9e8cb73839ac9f2f1"), "firstname" : "Yusuf", "lastname" : "Shakeel", "studentid" : "s01" } { "_id" : ObjectId("5d16c9efe8cb73839ac9f2f2"), "firstname" : "Jane", "lastname" : "Doe", "studentid" : "s02" } { "_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"), "firstname" : "John", "lastname" : "Doe", "studentid" : "s03" } { "_id" : ObjectId("5d16ca23e8cb73839ac9f2f4"), "firstname" : "Alice", "lastname" : "Doe", "studentid" : "s04", "score" : 10.5 } { "_id" : ObjectId("5d16ca23e8cb73839ac9f2f5"), "firstname" : "Bob", "lastname" : "Doe", "studentid" : "s05", "date_of_birth" : ISODate("2000-01-01T00:00:00Z") }
The pretty() method returns the result in easy-to-read format.
pretty()
In MongoDB we can have document inside a document (embedded/nested document).
In the following example we are inserting a new document in the students collection and it has a new field contact_phone which has its own field-value pairs.
contact_phone
> db.students.insertOne({ "firstname": "Eve", "lastname": "Doe", "studentid": "s06", "contact_phone": { "primary": { "number": "+919800000000", "name": "Bill Doe", "relation": "Father" }, "secondary": [ { "number": "+919800000001", "name": "Mac Doe", "relation": "Brother" } ] } });
> db.students.find().pretty(); { "_id" : ObjectId("5d16c9e9e8cb73839ac9f2f1"), "firstname" : "Yusuf", "lastname" : "Shakeel", "studentid" : "s01" } { "_id" : ObjectId("5d16c9efe8cb73839ac9f2f2"), "firstname" : "Jane", "lastname" : "Doe", "studentid" : "s02" } { "_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"), "firstname" : "John", "lastname" : "Doe", "studentid" : "s03" } { "_id" : ObjectId("5d16ca23e8cb73839ac9f2f4"), "firstname" : "Alice", "lastname" : "Doe", "studentid" : "s04", "score" : 10.5 } { "_id" : ObjectId("5d16ca23e8cb73839ac9f2f5"), "firstname" : "Bob", "lastname" : "Doe", "studentid" : "s05", "date_of_birth" : ISODate("2000-01-01T00:00:00Z") } { "_id" : ObjectId("5d16cfa4e8cb73839ac9f2f6"), "firstname" : "Eve", "lastname" : "Doe", "studentid" : "s06", "contact_phone" : { "primary" : { "number" : "+919800000000", "name" : "Bill Doe", "relation" : "Father" }, "secondary" : [ { "number" : "+919800000001", "name" : "Mac Doe", "relation" : "Brother" } ] } }
In the next tutorial we will learn to query documents.
See you there. Don't forget to share this tutorial. Have fun developing :)