Part 3 — MongoDB Step by Step — Data Types and Basic Operations

In the previous articles (article1, article2), we introduced ourselves with MongoDB overview, local setup and schema.

In this article we will focus on data types supported by MongoDB and some basic operations.

Let’s get started

Data Types

MongoDB supports many data types, some of them are:

· String − This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid.

· Integer − This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.

· Boolean − This type is used to store a boolean (true/ false) value.

· Double − This type is used to store floating point values.

· Timestamp − ctimestamp. This can be handy for recording when a document has been modified or added.

· Object ID − This datatype is used to store the document’s ID.

· Binary data − This datatype is used to store binary data.

· Code − This datatype is used to store JavaScript code into the document.

· Regular expression − This datatype is used to store regular expression.

· Min/ Max keys − This type is used to compare a value against the lowest and highest BSON elements.

· Arrays − This type is used to store arrays or list or multiple values into one key.

· Object − This datatype is used for embedded documents.

· Null − This type is used to store a Null value.

· Symbol − This datatype is used identically to a string; however, it’s generally reserved for languages that use a specific symbol type.

· Date − This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.

Create Database

For creating database in MongoDB the command you have to fire from client which is use Database_Name. This command creates a new database if not exist otherwise it will return the existing one.

Suppose we have to create database with name articles, then command would be:

use articles

If you want to check your database list then use command

show dbs

It will display the entire database list with their size. (Two default database admin and local already present).

To check currently selected database use command

db

It will print the name of currently selected database.

As you can see in image front command prompt is mongo client and back one is local mongo server running as described in article 1. First command is show dbs which display default databases (as we did not created any database till now), then we created a database using use articles command. After that we check our current database by db command.

Again we hit show dbs command, but it still shows only admin and local database. Where is my articles database? What is going on here? Is our database not created?

Slow down, everything is fine, our database created successfully. It doesn’t show up because it does not have any document. We have to insert at least one document to show this database in list.

So let’s insert a document in our Articles !

Before inserting document we have to create collection (Table in RDBMS) in our article database.

Create Collection

For creating the collection in articles database, first we have to move to articles database (if not already) by using use articles command. After that use command

db.createCollection(“Post”)

It will create your collection with name Post.

There are many option parameter you can pass in create collection command db.createCollection(name, options), in details here.

Can We Insert Now?

Yes now we can insert document in Post collection of articles database. For insertion document we can use either command

insert() or save()

Now if we run the show dbs command, we will get our articles database in list

Insert() VS Save()

To insert document we can use db.Post.insert(document) or db.Post.save(document).

The actual difference is if we don’t specify _id then save will work exactly same as insert (insert document). If we specify _id then save method will replace whole data of that document which have same id, otherwise insert it.

What the heck is this _id?

If we do not provide any _id at the time of inserting document, then MongoDB create one for every document which is unique for each.

_id is 12 byte hexadecimal number unique for every document in a collection. 12 byte devided as follows:

_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)

Insert Multiple Documents in single query

Inserting multiple documents is as simple and same as the single document, Using JSON array.

Delete Collection

For deleting any collection we have to enter db.collection.drop() , Like in our case:

db.Post.drop()

Delete Database

For deleting any database (not default one) we have to use .

db.dropDatabase()

For above to operations we must be in our articles database.

I am closing this part here, and soon will write the next parts with more operations on MongoDB.

Thanks for being till here. If you find this helpful, be sure to click ❤ recommend. It means a lot.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.