Christopher Glikpo
Posted on September 13, 2021
Introduction
In this article, I am going to introduce Mongodb,step-by-step basic implementation and explanation.
This article covers the following areas of MONGODB.
- Introduction of MongoDB
- Installation of MongoDB
- MongoDB terms vs SQL terms
- Brief overview of how MongoDB works
- Adding items to a database
Introduction of MongoDB
MongoDB is an open-source document-based database management tool that stores data in JSON-like formats. It is a highly scalable, flexible, and distributed NoSQL database.
Installation of MongoDB
You need to install MongoDB on your computer before you can connect to it. You can install MongoDB by following these instructions Mac and Windows.
Try entering mongod --version
into your command line once you've finished the installation procedure. You should receive anything along these lines:
mongod --version
To Check MongoDB Shell version:
mongo --version
STARTING MONGODB
You can start MongoDB in the command line with the mongod
command.
By default, mongodb server will start at port 27017
mongod
Keep the mongod
window running when you want to work with your local MongoDB. MongoDB stops when you close the window
MongoDB vs SQL Terminology
Terminology | Description |
---|---|
field | A name-value pair. It is similar concept of column of an RDBMS |
document | A group of fields are termed as document. In RDBMS we will term it as row. MongoDB document follows JSON syntax but it is a BSON syntax (BSON is an extended version of JSON implemented by MongoDB). |
collection | A group of documents is called collection in MongoDB. A collection is similar concept of table of an RDBMS. |
database | A Physical Container for Collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases |
My most recent client, a large stockbroking firm, uses MongoDB in addition to SQL Server. Recently, I had the opportunity to work with their MongoDB Expert, who is also in charge of one of their SQL Servers, which was underperforming.
The terminology we used for various database elements presented the biggest challenge for both of us when discussing the database. While I was referring to a record or row, he was referring to a document, and we were frequently lost in translation. To avoid any misunderstandings, I've created a small table that maps MongoDB terms to SQL terms.
SQL Terms | MongoDB Terms |
---|---|
database | database |
table | collection |
row | document or BSON document |
column | field |
Index | Index |
table joins | embedded documents and linking |
primary key.Specify any unique column or column combination as primary key. | primary key.In MongoDB, the primary key is automatically set to the _id field. |
aggregation (e.g. group by) | aggregation pipeline |
SQL Concepts | MongoDB Aggregation Operators |
---|---|
WHERE | $match |
GROUP BY | $group |
HAVING | $match |
SELECT | $project |
ORDER BY | $sort |
LIMIT | $limit |
SUM() | $sum |
COUNT() | $sum |
join | $lookup |
Brief overview of how MongoDB works
MongoDB lets you store things (called documents) inside databases. Each database contains multiple collections.
To make it easier to understand, you can think of MongoDB as a institutions. It contains many element.
Each element is a database. Each database is responsible for storing information about one application. You can store as much information as you want.
Let take the school for instance.Each department is a collection. Each collection can only contain one type of data.
For example, one collection can be used for instructors, one collection for users, one collection for courses, and so on.
Adding items to a database
One way to add items to a MongoDB database is through the Mongo Shell. To open up the Mongo Shell, you open another command line window and run mongo
.
mongo
Note: Make sure you keep the mongod window open! You won't be able to interact with the Mongo Shell if you close the mongod window.
First, we need a database to work with. You can see the currently selected database with the db command. (By default, you should on the test database).
> db
Note: The > in the code above signifies the Mongo Shell. You don't need to type >. It is not part of the command.
For this article, we'll create a database called tweety
. You can use the use <database>
command to create and switch to a new database.
use tweety
We're going to add a user into the tweety database. Here, we need to put the user into a collection. We'll use users as the name of the collection.
To add an item to a collection, you can pass a JavaScript object into db.collectionName.insertOne().
db.users.insertOne({ firstName:"Christopher",
lastName:"Glikpo"
});
Let's add one user into the database before we continue.
db.users.insertOne({ firstName:"Ben",
lastName:"Attoh"
});
You can see the users we've added by using the find command. db.collectionName.find().
db.characters.find();
If you've reached this point, thank you very much. I hope that this tutorial has been helpful for you and I'll see you all in the next.
If you like my work, please consider
so that I can bring more projects, more articles for you
If you want to learn more about Web Development don't forget to to follow me on Youtube!
Posted on September 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.