Overview of MongoDB

Faisal Khan
2 min readMay 30, 2019

--

MongoDB is one of the widely used document database. It obeys the C and Ain the CAP thoerem. MongoDB is heavily used with NodeJS due to its JSON friendly API.

MongoDB: url

Installation instructions are available at Mongo Official website: https://www.mongodb.com. Let’s consider a few commands using the MongoDB CLI.

Entering the CLI: mongo and press enter. If installation is successful you should see the following output. Usually mongo DB runs on port 27017.

# mongoMongoDB shell version v3.4.5connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.5Server has startup warnings:2017-09-15T11:42:04.673+0000 I STORAGE  [initandlisten]2017-09-15T11:42:04.673+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine2017-09-15T11:42:04.673+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem2017-09-15T11:42:05.313+0000 I CONTROL  [initandlisten]2017-09-15T11:42:05.313+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.2017-09-15T11:42:05.313+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.2017-09-15T11:42:05.313+0000 I CONTROL  [initandlisten]
>

Show databases: show dbs

Initiating, selecting a database and performing insertions.

Inserting
Enter use mydb and a new database name mydb will be created. Unlike tables in MySQL, Mongo has collections. Documents (JSON) are inserted into such collections. Let’s create the dogs collection and add a dog name Molly with weight 50kg. Since we have selected mydb we can call it as db and perform operations.

db.dogs.insert({name: ‘Molly’, weight:50})

Viewing data
Using the pretty method formats the output for a more readable format. As you can see mongo add an id name _id as an index to each of the documents.

> db.dogs.find()
{ "_id" : ObjectId("59bbce8288b6c364cefd9de6"), "name" : "Molly", "weight" : 50 }
> db.dogs.find().pretty()
{
"_id" : ObjectId("59bbce8288b6c364cefd9de6"),
"name" : "Molly",
"weight" : 50
}
>

Updating data
The command below updates the entry we made and set the name to Tommy.

> db.dogs.update({_id: ObjectId('59bbce8288b6c364cefd9de6')},
{$set: {name:'Tommy'}})

Deleting data
Lets delete what we have inserted

> db.dogs.remove({_id:ObjectId('59bbce8288b6c364cefd9de6')})
WriteResult({ "nRemoved" : 1 })
>

Those are the basic operations of any database, or simply the CRUD operations.

C — Create
R — Read
U — Update
D — Delete

--

--