1. A. D. Patel Institute Of Technology
Big Data and Analytics [2171607] : A. Y. 2019-20
Installing MongoDB , Querying through MongoDB &
MongoDB through JavaScript Shell
Prepared By :
Dhruv V. Shah (160010116053)
B.E. (IT) Sem - VII
Guided By :
Dr. Narendra Chauhan
(Head Of IT Dept. , ADIT)
Department Of Information Technology
A.D. Patel Institute Of Technology (ADIT)
New Vallabh Vidyanagar , Anand , Gujarat
1
2. Outline
Introduction
Why Use MongoDB ? & What is MongoDB great for?
MongoDB : CAP Approach.
Steps for MongoDB Installation
MongoDB CRUD Operations
MongoDB through JavaScript Shell
References
2
3. Introduction
What is MongoDB ?
MongoDB is a general purpose, document-based, distributed database built for modern
application developers and for the cloud era.
MongoDB is a document database, which means it stores data in JSON-like documents (called
BSON [Binary JavaScript Object Notation]).
It falls under the category of a NoSQL database.
MongoDB is
Scalable High-Performance Open-source, Document-orientated database.
Built for Speed.
Rich Document based queries for Easy readability.
Full Index Support for High Performance.
Replication and Failover for High Availability.
Auto Sharding for Easy Scalability.
Map / Reduce for Aggregation.
3
4. Cont..
4
Fig 1. Terms of SQL & MongoDB
Key points of MongoDB
1. Develop Faster
2. Deploy Easier
3. Scale Bigger
In MongoDB , Fields contains set of key & value pair.
5. Why use MongoDB ?
MongoDB stores documents (or) objects.
Now-a-days, everyone works with objects (Python/Ruby/Java/etc.).
And we need Databases to persist our objects. Then why not store objects directly ?
Embedded documents and arrays reduce need for joins. No Joins and No-multi document
transactions.
5
What is MongoDB great for?
RDBMS replacement for Web Applications.
Semi-structured Content Management.
Real-time Analytics & High-Speed Logging.
Caching and High Scalability
Web 2.0, Media, SAAS, Gaming, HealthCare, Finance, Telecom, Government
6. MongoDB : CAP Approach
Focus on Consistency and Partitiontolerance.
Consistency
all replicas contain the same version of the data.
Availability
system remains operationalon failing nodes.
Partition Tolerance
multiple entry points
system remains operationalon system split
6
C
A P
CAP Theorem:
satisfying all three at the same time
is impossible.
7. Steps for MongoDB Installation
7
Link For MongoDB : https://www.mongodb.com/download-center/enterprise
15. 15
Suppose in my laptop MongoDB location is as below :
Path : C:Program FilesMongoDBServer4.2bin
In window, we can write this path as follow :cd C:Program FilesMongoDBServer4.2bin
and then after write mongo.exe then press enter to start the mongoDB.
Cont..
Fig. MongoDB Terminal
17. Create or insert operations add new documents to a collection.
If the collection does not currently exist, insert/save operations will create the collection.
MongoDB provides the following methods to insert documents into a collection :
1) db.collection_name.insertOne() .
2) db.collection_name.insertMany() .
Other than this two methods one another method also available for creating collections which is
written below:
db.createCollection(“collection_name”) .
17
Create Operations
21. Read operations retrieves documents from a collection; i.e. queries a collection for documents.
MongoDB provides the following methods to read documents from a collection:
1) db.collection_name.find() .
If we execute the above query for read or display the collection data then it is not produce output
in human readable form or we can say that it produces output in linear sequence.
If we want to display the output in humane readable or well formatted way then write below
command:
db.collection_name.find().pretty() .
21
Read Operations
23. Update operations modify existing documents in a collection.
MongoDB provides the following methods to update documents of a collection:
1) db.collection_name.updateOne() :
This command is use for updating or modifying document value or add a new key
attribute into the existing collection at a time only one updated.
2) db.collection_name.updateMany() :
This command is use for updating or modifying document value or add a new key
attribute into the existing collection at a time multiple updated.
3) db.collection_name.replaceOne() :
This method actually work by replacing entire existing document with new
document. It is not use for modifying the existing document.
In MongoDB, update operations target a single collection. All write operations in MongoDB are
atomic on the level of a single document.
23
Update Operations
27. Delete operations remove documents from a collection.
MongoDB provides the following methods to delete documents of a collection:
1) db.collection_name.deleteOne() :
This command is use for removing or deleting document from the collection at a
time only one deleted.
2) db.collection_name.deleteMany() :
This command is use for removing or deleting document from the collection at a
time many deleted.
In MongoDB, delete operations target a single collection.
All write operations in MongoDB are atomic on the level of a single document.
27
Delete Operations
30. The MongoDB shell is an interactive JavaScript shell.
As such, it provides the capability to use JavaScript code directly in the shell or executed as a
standalone JavaScript file.
You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or
perform administrative operation.
30
MongoDB through JavaScript Shell
31. OPENING NEW CONNECTIONS
From the mongo shell or from a JavaScript file, you can instantiate database connections
using the Mongo() constructor:
new Mongo()
new Mongo(<host>)
new Mongo(<host: port>)
Consider the following example that instantiates a new connection to the MongoDB instance
running on localhost on the default port and sets the global db variable to myDatabase using
the getDB() method:
conn = new Mongo();
db = conn.getDB("myDatabase");
31
Cont...
33. db.adminCommand()
db.adminCommand() runs commands against the admin database regardless of the database
context in which it runs.
EXAMPLE: The following example uses db.adminCommand() to execute the
renameCollection administrative database command to rename the orders collection in the
test database to orders-2016.
db.adminCommand (
{
renameCollection: "test.orders",
to: "test.orders-2016“
}
)
33
Cont...
34. 34
Cont...
db.getCollectionName()
Returns an array containing the names of all collections and views in the current database, or
if running with access control, the names of the collections according to user’s privilege.
EXAMPLE: The following returns the names of all collections in the records database:
use records
db.getCollectionNames()
The method returns the names of the collections in an array:
[ "employees", "products", "mylogs", "system.indexes" ]
35. 35
Cont...
db.getUsers()
Returns information for all the users in the database.
EXAMPLE:
To view all users for the current database who have SCRAM-SHA-256 credentials:
db.getUsers({ filter: { mechanisms: "SCRAM-SHA-256" } })
36. 36
Cont...
Scripting
From the system prompt, use mongo to evaluate JavaScript.
1) --eval option :
Use the --eval option to mongo to pass the shell a JavaScript fragment, as in the
following:
mongo test --eval "printjson(db.getCollectionNames())"
2) Execute a JavaScript file :
You can specify a .js file to the mongo shell, and mongo will execute the JavaScript
directly. Consider the following example:
mongo localhost:27017/test myjsfile.js
This operation executes the myjsfile.js script in a mongo shell that connects to the test
database on the mongod instance accessible via the localhost interface on port 27017.
3) Load a JavaScript :
This function loads and executes the myjstest.js file.
You can execute a .js file from within the mongo shell, using the load() function, as in the
following:
load("myjstest.js")