Mongodb From Scratch: Mastering Nosql Database
Mongodb From Scratch: Mastering Nosql Database
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
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.
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
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>mongo.exe
Setting up the Environment
Setting up the Environment
Setting up the Environment
Setting up the Environment
db.help()
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
Syntax
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
>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.
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
Note
db.dropDatabase()
This will delete the selected database. If you have not selected
any database, then it will delete default 'test' database.
Example
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>, then
dropDatabase() command would be as follows −
>db.dropDatabase()
>
Now check list of databases.
test 0.23012GB
>
The createCollection() Method
db.createCollection(name, options)
The createCollection() Method
db.createCollection(name, options)
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
>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 −
"ok" : 0,
"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
db.COLLECTION_NAME.drop()
The drop() Method
db.COLLECTION_NAME.drop()
Example
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
>
Integer − This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.
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.
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.
Code − This datatype is used to store JavaScript code into the document.
>db.COLLECTION_NAME.insert(document)
Example
> db.users.insert({
Database ... _id : ObjectId("507f191e810c19729de860ea"),
... })
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