DEV Community

Cover image for GETTING STARTED WITH MONGODB
Christopher Glikpo
Christopher Glikpo

Posted on

GETTING STARTED WITH MONGODB

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
Enter fullscreen mode Exit fullscreen mode

MongoDB Command

To Check MongoDB Shell version:

mongo --version
Enter fullscreen mode Exit fullscreen mode

MongoDB Shell

STARTING MONGODB

You can start MongoDB in the command line with the mongod command.
By default, mongodb server will start at port 27017

mongod
Enter fullscreen mode Exit fullscreen mode

Mongod Cmd

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
Enter fullscreen mode Exit fullscreen mode

Mongo Command

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
Enter fullscreen mode Exit fullscreen mode

Note: The > in the code above signifies the Mongo Shell. You don't need to type >. It is not part of the command.

DB test

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

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"
});
Enter fullscreen mode Exit fullscreen mode

Test 1

Let's add one user into the database before we continue.

db.users.insertOne({ firstName:"Ben",
                     lastName:"Attoh"
});
Enter fullscreen mode Exit fullscreen mode

Alt Text
You can see the users we've added by using the find command. db.collectionName.find().

db.characters.find();
Enter fullscreen mode Exit fullscreen mode

Alt Text

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
Buy me a coffee
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!

Top comments (1)

Collapse
 
obaino82 profile image
Obaino82

,๐Ÿ‘