3. Introduction
MongoDB is a cross-platform, document oriented database
It is a NoSQL database
It is open source
It provides high performance and scalability
It stores data in the form of key/value pairs (document)
It eliminates the need for Object Relational Mapping (ORM) in database development
4. MOngoDB stores data in form of BSON (binary JavaScript Object Notation) documents
{
name: “travis”,
salary: 30000,
designation: “Computer Scientist”,
teams: [ “front-end”, “database” ]
}
6. Important Features of MongoDB?
•Queries support: MongoDB supports ad-hoc and document-based queries.
•Index Support: All fields in the document are indexed.
•Replication: MongoDB possesses Master-Slave replication. It uses a native
application to preventing database downtime by maintaining multiple copies of
data.
•Multiple Servers: Duplicate data that is stored in the database is run over multiple
servers to avoid the damage caused due to hardware failure.
•Auto-sharding: The data is being distributed among several physical partitions
known as shards. MongoDB has an in-built feature called automatic load balancing.
•MapReduce: It is a flexible aggregation tool that supports the MapReduce
function.
7. •Failure Handling: In MongoDB, works effectively in case of failures such as
multiple machine failures, data center failures by protecting data and making it
available.
•GridFS: This feature will allow the files to divide into smaller parts and store them
in different documents without complicating the stack.
•Schema-less Database: MongoDB is a schema-less database programmed in C++
language.
•Document-oriented Storage: It uses BSON format which is similar to JSON
•Procedures: MongoDB JavaScript works better than procedures as databases use
the language more than procedures.
8. NoSQL Database
•It is a non-relational database.
• No need to create tables, relations for storing data .
•This type of database is used to store web applications or large databases.
10. MongoDB does not have schema: We can insert the data in any order.
◦ We can see in the screenshot that the first collection (tuple) in the document (table) newdb the first tuple
has fields name and age while the second tuple had gender and name it is not mandatory to maintain a
structure in MongoDB.
13. Key Terms
Collection
◦ It 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.
Document
◦ A document is a set of key-value pairs.
◦ It 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.
14. Installation Process
Cloud : MongoDB Atlas
Stand-alone system – link
Install shell
Install compass
Install data tool (if needed)
15. Execution Process
◦ Run MongoDB’s database server from command prompt
◦ If we run the command “mongod”, it will activate the mongoDB database server which is running at port 27017.
16. ◦ Run MongoDB’s shell
◦ We run the “mongo” file, which is the MongoDB Shell, from a new command prompt window.
◦ This is where we feed the database server with commands such as creating a database or a collection and dropping collections.
17. Create Database
MongoDB uses the ‘use’ command to create a database.
If the database exists already, then it will be returned. Otherwise, a new database with the given
database name will be created.
Syntax: use <DATABASE_NAME>
Example : To create a database ‘movie’.
◦ use movie //This will create a new database called ‘movie’.
18. Drop Database
MongoDB uses the ‘db.dropDatabase()’ command to drop a database.
If the database exists already, then it will be dropped and true will be returned. Otherwise,
nothing will be dropped and 0 will be returned.
Syntax: db.dropDatabase()
Example:
◦ db.dropDatabase ()
19. Create Collection
A collection is what is referred to as a table in normal RDBMS.
It stores documents which may not be in the same structure.
A collection has various options such as setting maximum size and validating rules.
20. Syntax:
db.createCollection(name,options)
◦ Parameter :Name
This field is used to specify the name of the collection which you are creating.
◦ Parameter :Options
This field is used to specify particular configurations for the collection which you
have created.
21. Example
To create a database ‘movie’.
use movie //This will create a new database called ‘movie’.
To create collection ‘KMovies’
db.createCollection("KMovies", { capped : true, autoIndexId : true,
size :6142800, max : 10000 } )
This will now create a collection KMovies which is capped, auto indexed with specified size and
specified maximum number of documents.
22. This will now create a collection KMovies with capping, auto indexing, we will set the size to
6124800 and we will set the maximum number of documents to 10000.
23. Drop Collection
Since there are several collections in a particular database, we need to specify to MongoDB which collection we
are aiming to drop.
We use “show collections” to find out which all collections we have in our database.
Syntax: db.COLLECTION_NAME.drop()
Example : db.KMovies.drop()
24. Insert Document
This is used to insert several documents(tuples) into a collection(table)
If the collection is not available a new collection is created with the specified collection name
and documents are inserted into it.
Syntax
◦ db.collection_name.insert(document)
◦ // document parameter includes the column name and their values respectively, the rows are separated using {} and comma
operators.
29. Pretty() Command
This is used to display the contents of collection in a formatted way.
Syntax
◦ db.collection_name.find().pretty()
◦ //where collection_name(table name) is the name of the collection.
32. Delete Document
This is used to delete the value of a certain column in a collection
Syntax
db.collection_name.remove(selection_criteria)
//where collection_name(table name) is the name of the collection, selection_criteria is which value should be deleted.