0% found this document useful (0 votes)
117 views58 pages

Mongodb From Scratch: Mastering Nosql Database

The document discusses MongoDB, which is a cross-platform, document-oriented database that provides high performance, high availability, and easy scalability. It works on the concepts of collections and documents. Key terms discussed include databases, collections, and documents. Databases contain collections and each database gets its own set of files. Collections are groups of documents and are equivalent to tables in a relational database. Documents are like rows in a table, with a dynamic schema of key-value pairs.

Uploaded by

arpit sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
117 views58 pages

Mongodb From Scratch: Mastering Nosql Database

The document discusses MongoDB, which is a cross-platform, document-oriented database that provides high performance, high availability, and easy scalability. It works on the concepts of collections and documents. Key terms discussed include databases, collections, and documents. Databases contain collections and each database gets its own set of files. Collections are groups of documents and are equivalent to tables in a relational database. Documents are like rows in a table, with a dynamic schema of key-value pairs.

Uploaded by

arpit sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 58

MongoDB

from
Scratch
MASTERING NOSQL
D ATA B A S E
MongoDB is a cross-platform, document oriented database that provides, high
performance, high availability, and easy scalability. MongoDB works on concept of
collection and document.

Database

Database is a physical container for collections. Each database gets its own set of files

Hello on the file system. A single MongoDB server typically has multiple databases.

Collection
MongoDB Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table.
A collection exists within a single database. Collections do not enforce a schema.
Documents within a collection can have different fields. Typically, all documents in a
collection are of similar or related purpose.

Document

A document is a set of key-value pairs. Documents have dynamic schema. Dynamic


schema means that documents in the same collection do not need to have the same set
of fields or structure, and common fields in a collection's documents may hold different
types of data.
Relationship of RDBMS terminology with MongoDB

RDBMS MongoDB
Database Database

Hello Table
Tuple/Row
Collection
Document
MongoDB column Field
Table Join Embedded Documents
Primary Key (Default key _id
Primary Key
provided by MongoDB itself)
Database Server and Client
mysqld/Oracle mongod
mysql/sqlplus mongo
Setting up the Environment
Download MongoDB

https://www.mongodb.com/download-center.

Install MongoDB On Windows

Now install the downloaded file, by default, it will be installed in the folder C:\Program Files\.

MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is c:\
data\db. So you need to create this folder using the Command Prompt. Execute the following command
sequence.

C:\>md data

C:\md data\db
Setting up the Environment

Then you need to specify set the dbpath to the created directory in mongod.exe. For the same, issue the following
commands.

In the command prompt, navigate to the bin directory current in the MongoDB installation folder. Suppose my
installation folder is C:\Program Files\MongoDB

C:\Users\91981>cd C:\Program Files\MongoDB\Server\4.4\bin

C:\Program Files\MongoDB\Server\4.4\bin>mongod.exe --dbpath "C:\data“

This will show waiting for connections message on the console output, which indicates that the mongod.exe process
is running successfully.
Setting up the Environment
Now to run the MongoDB, you need to open another command prompt and issue the following command.

C:\Program Files\MongoDB\Server\4.4\bin>mongo.exe
Setting up the Environment
This will show that MongoDB is installed and run successfully. Next time when you run MongoDB,
you need to issue only commands.

C:\Program Files\MongoDB\Server\4.4\bin>mongod.exe --dbpath "C:\data"

C:\Program Files\MongoDB\Server\4.4\bin>mongo.exe
Setting up the Environment
Setting up the Environment
Setting up the Environment
Setting up the Environment
db.help()

MongoDB To get a list of commands, type db.help() in MongoDB


client. This will give you a list of commands
Help
db.stats()

To get stats about MongoDB server, type the command db.stats() in MongoDB
client. This will show the database name, number of collection and documents in the

MongoDB database.

Statistics
The use Command

Creating MongoDB use DATABASE_NAME is used to create


database. The command will create a new database if it
Database doesn't exist, otherwise it will return the existing database.

Syntax

Basic syntax of use DATABASE statement is as follows −

use DATABASE_NAME
Example

Creating If you want to use a database with name <mydb>, then use
DATABASE statement would be as follows −
Database
>use mydb

switched to db mydb
To check your currently selected database, use the command
db

Creating >db
Database mydb

If you want to check your databases list, use the command


show dbs.

>show dbs

local 0.78125GB

test 0.23012GB
Your created database (mydb) is not present in list. To display database,
you need to insert at least one document into it.

Creating >db.movie.insert({"name":“Krishna kumar singh"})

Database >show dbs

local 0.78125GB

mydb 0.23012GB

test 0.23012GB
Note

Creating In MongoDB default database is test. If you didn't create


any database, then collections will be stored in test
Database database.
The dropDatabase() Method

Dropping MongoDB db.dropDatabase() command is used to drop a


existing database.
Database Syntax

Basic syntax of dropDatabase() command is as follows −

db.dropDatabase()

This will delete the selected database. If you have not selected
any database, then it will delete default 'test' database.
Example

Dropping First, check the list of available databases by using the


command, show dbs.
Database
>show dbs

local 0.78125GB

mydb 0.23012GB

test 0.23012GB

>
If you want to delete new database <mydb>, then
dropDatabase() command would be as follows −

Dropping >use mydb


Database switched to db mydb

>db.dropDatabase()

>{ "dropped" : "mydb", "ok" : 1 }

>
Now check list of databases.

Dropping >show dbs

Database local 0.78125GB

test 0.23012GB

>
The createCollection() Method

Creating MongoDB db.createCollection(name, options) is used to


create collection.
Collection Syntax

Basic syntax of createCollection() command is as follows −

db.createCollection(name, options)
The createCollection() Method

Creating MongoDB db.createCollection(name, options) is used to


create collection.
Collection Syntax

Basic syntax of createCollection() command is as follows −

db.createCollection(name, options)

In the command, name is name of collection to be created.


Options is a document and is used to specify configuration
of collection.
The createCollection() Method
Creating
Collection MongoDB db.createCollection(name, options) is used to
Parameter Type Description create collection.

Syntax
Name of the collection
Name String
to be created
Basic syntax of createCollection() command is as follows −

(Optional) Specify
Docum db.createCollection(name, options)
Options options about memory
ent
size and indexing
In the command, name is name of collection to be created.
Options is a document and is used to specify configuration
of collection.
Options parameter is optional, so you need to specify only
the name of the collection. Following is the list of options
you can use −
Creating Field Type Description

Collection (Optional) If true, enables a capped collection.


Capped collection is a fixed size collection that
capped Boolean automatically overwrites its oldest entries when
it reaches its maximum size. If you specify true,
you need to specify size parameter also.

(Optional) If true, automatically create index on


autoIndexId Boolean
_id field.s Default value is false.

(Optional) Specifies a maximum size in bytes


size number for a capped collection. If capped is true, then
you need to specify this field also.

(Optional) Specifies the maximum number of


max number
documents allowed in the capped collection.
Examples

Basic syntax of createCollection() method without options is as follows −

Creating >use test


Collection switched to db test

>db.createCollection("mycollection")

{ "ok" : 1 }

>

You can check the created collection by using the command show collections.

>show collections

mycollection

system.indexes
The following example shows the syntax of
createCollection() method with few important options −

Creating > db.createCollection("mycol", { capped : true,


Collection autoIndexID : true, size : 6142800, max : 10000 } ){

"ok" : 0,

"errmsg" : "BSON field 'create.autoIndexID' is an unknown


field.",

"code" : 40415,

"codeName" : "Location40415"

>
In MongoDB, you don't need to create collection. MongoDB
creates collection automatically, when you insert some document.
Creating
Collection >db.tutorialspoint.insert({"name" : "tutorialspoint"}),

WriteResult({ "nInserted" : 1 })

>show collections

mycol

mycollection

system.indexes

tutorialspoint

>
The drop() Method

Dropping MongoDB's db.collection.drop() is used to drop a collection


from the database.
Collection Syntax

Basic syntax of drop() command is as follows −

db.COLLECTION_NAME.drop()
The drop() Method

Dropping MongoDB's db.collection.drop() is used to drop a collection


from the database.
Collection Syntax

Basic syntax of drop() command is as follows −

db.COLLECTION_NAME.drop()
Example

First, check the available collections into your database mydb.


Dropping
Collection >use mydb

switched to db mydb

>show collections

mycol

mycollection

system.indexes

tutorialspoint

>
Now drop the collection with the name mycollection.

>db.mycollection.drop()

Dropping true

>
Collection Again check the list of collections into database.

>show collections

mycol

system.indexes

tutorialspoint

>

drop() method will return true, if the selected collection is


dropped successfully, otherwise it will return false.
MongoDB
Data Types
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.

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.

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

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.

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.


The insert() Method

Database To insert data into MongoDB collection, you need to use


MongoDB's insert() or save() method.
Insert Syntax

The basic syntax of insert() command is as follows −

>db.COLLECTION_NAME.insert(document)
Example

> db.users.insert({
Database ... _id : ObjectId("507f191e810c19729de860ea"),

Insert ... title: "MongoDB Overview",

... description: "MongoDB is no sql database",

... by: "tutorials point",

... url: "http://www.tutorialspoint.com",

... tags: ['mongodb', 'database', 'NoSQL'],

... likes: 100

... })

WriteResult({ "nInserted" : 1 })

>
MongoDB Compass
M O N G O D B C O M PA S S I S A G U I F O R M O N G O D B . I T I S A L S O K N O W N A S M O N G O D B G U I .
M O N G O D B A L L O W S U S E R S TO A N A LY Z E T H E C O N T E N T O F T H E I R S TO R E D D ATA W I T H O U T
A N Y P R I O R K N O W L E D G E O F M O N G O D B Q U E RY S Y N TA X . W H E N W E E X P L O R E E X P L O R I N G
O U R D ATA I N T H E V I S U A L E N V I R O N M E N T, W E C A N U S E C O M PA S S G U I TO O P T I M I Z E
P E R F O R M A N C E , M A N A G E I N D E X E S , A N D I M P L E M E N T D O C U M E N T- VA L I D AT I O N .
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Compass
MongoDB Atlas
MongoDB Atlas
MongoDB Atlas
MongoDB Atlas
MongoDB Atlas
MongoDB Atlas
MongoDB Atlas
MongoDB Atlas
Q and A
Thanks

You might also like