This entire week, I am going to discuss the Fundamentals of MongoDB. Today is the second post in the six post series. We are going to learn about CRUD Operation: Creating Objects in MongoDB. I have recently build a Video Course at Foundations of Document Databases with MongoDB focusing on helping users to get started with MongoDB quickly and efficiently.
CRUD Operation: Creating Objects
CRUD is an acronym and it stands for Create, Read, Update, and Delete. MongoDB is a NoSQL database and that means along with SQL concepts, it is also following NoSQL concepts.
For MongoDB Creation operations actually stands for inserting new documents as well as creating new documents to the collection.
Here are the two most popular CREATE methods in MongoDB.
db.collection.insertOne() – Inserts a single document in the collection.
db.collection.insertMany() – Inserts multiple documents in the collection.
If the collection does not currently exist, insert operations will create the collection instead of giving the error.
Create Database
In MongoDB creation of the database is interesting behavior. The database is not created till the first collection is not created and the collection is usually not created until the first document is not inserted. I will write a blog post about this in detail in the future. However, let us learn the basic steps to create the database first.
To create a database, first, connect to your MongoDB cluster (it can be installed locally as well as on the cloud). I prefer to use MongoDB Altas which is a cloud service provided by MongoDB.
The command to connect to your MongoDB Atlast is very simple.
mongo "YourConnectionString" --username newuser --password newpassword
Once you connect with MongoDB Atlas, you can run the following command to display the current database context.
db
If you have just connected with the cluster, it may just display a “test” as a current database.
You can check all the databases on your server by running the following command.
show dbs
Now let us assume that you have to create a new database called “sqlauthority”.
In MongoDB, there is no command to create a database explicitly. You can just use the following command and it will change the context of the execution to the new database.
use sqlauthority
In general, we expect that when we run the above command it should give us error but it will not give us error and silently change the context of the database, giving us the impression that it is now going to use the new database.
While we all like to believe that it has created the database in the system, it is actually not true. The name of the database is still held in the memory and the database is created when the first document inserted into the collection or the collection is created explicitly.
Create Collection
If you are from the relational background, I must say introduce MongoDB collections as SQL Server tables.
If you are a MongoDB expert and using it for a while, I am very confident that you may not have created a collection in the recent past. In MongoDB, most of the collection is created when the very first document is inserted.
However, it is also possible to create a new collection by running the following command. Let us create a new collection with the name “mongodbuser”.
db.createCollection("newusers")
When you run the above command MonogDB will create an empty collection named mongodbuser in the database sqlauthority.
You can list all the available collections by running the following command.
show collections
Now that we have created a database and collection, it is time to create our very first document.
Create Document – Creating Objects
Creating the document is one of the most frequent task by any MonogoDB user and rightly fit as a C from the CRUD operators. Let us create our very first document in our collection mongodbuser.
db.mongodbuser.insertOne( { "DisplayName": "Pinal Dave", "UserName": "pinaldave", "Job": { "Title": "DBA", "Area": "Database Performance Tuning" }, "Programming Languages": ["T-SQL", "PL/SQL", "SQL"] } )
When you run the above command it will insert a single document in mongodbuser with DisplayName field “Pinal Dave”.
Well, just like insertOne you can also use insertMany to insert multiple values and Creating Objects. When you use insertMany operator, you have to provide multiple JSON objects together.
You can either use find() operator to see the inserted data or can use MongoDB Compass for the same. I will blog more about this in future blog posts when we discuss Retrieve part of the MongoDB in tomorrow’s blog post.
Related Learning
Here are some of the relevant blog posts on MongoDB.
- Foundations of Document Databases with MongoDB – Video Course
- SQL Terms vs MongoDB Terms
- MongoDB Compass – Missing a Schema Section
- MongoDB Fundamentals – Getting Started – Day 1 of 6
Technology Online
I hope you find these Learning paths helpful. If you have a Pluralsight subscription, you can watch it for free. If you do not have a Pluralsight subscription, you can still watch the course for FREE by signing up for a trial account. Please note that you do not need any credit card. You can always connect with me on twitter.
Reference: Pinal Dave (https://blog.sqlauthority.com)
First appeared on MongoDB Fundamentals – CRUD: Creating Objects – Day 2 of 6