Mongo DB-CRUD
Mongo DB-CRUD
Reference:https://www.cloudduggu.com/mongodb/sharding
NoSQL Database:
NoSQL databases are called "Not Only SQL" or "Non-relational" databases. NoSQL databases
store and retrieve data other than tabular relations such as relation databases.
NoSQL databases include MongoDB, HBase, and Cassandra.
There are following properties of NoSQL databases.
❖ Design Simplicity
❖ Horizontal Scaling
❖ High Availability
NoSQL databases come in a variety of types based on their data model. The main types are
document, key-value, wide-column, and graph. They provide flexible schemas and scale easily
with large amounts of data and high user loads.
Each NoSQL database has its own unique features. At a high level, many NoSQL databases have
the following features:
▪ Flexible schemas
▪ Horizontal scaling
▪ Fast queries due to the data model
▪ Ease of use for developers
● An intuitive data model that is fast and easy for developers to work with.
● A flexible schema that allows for the data model to evolve as application needs change.
● The ability to horizontally scale out.
Because of these advantages, document databases are general-purpose databases that can be
used in a variety of use cases and industries.
MongoDB – Architecture
MongoDB was the startup of 10gen, which originated in 2007. Coming from the family
of Document stores, it is one of the typical NoSQL, schema-free databases with comparatively
high performance, scalability, and is rich in data processing functions. This open-source
database is written in C++ and makes use of dynamic schemas. The architecture
of MongoDB contains documents grouped into collections based on their structure. This
database makes use of BSON. BSON is the binary representation of JSON and supports
document storage and data interchange. In MongoDB, business subjects can be stored in a
VEC – CSE IV Semester – II Year – 19IT205T – DBMS
Along with the above-mentioned capabilities of MongoDB, it also provides a large replica sets
collection where each set can contain more than one copy of data. In the replica sets, all primary
functions (read and write) are performed on the primary set while secondary sets are used in
case of failure of the former one. MongoDB incorporates sharding, which makes use of the
scaling process horizontally. The load balancing property of this document store database is
justified by the fact that it runs on multiple servers, thereby providing duplication of data and
balancing of the load. In return, it also provides backup during the hardware failure. It also
makes use of a grid file system which divides the particular file into different parts and stores
them separately.
● The data model design reduces the need for joins and provides easy evolution of schema.
● High performance, as it contains neither join nor transactions which provide fast accessing
and hence performance is increased.
● High availability due to the incorporation of replica sets that are able to provide backup
during failures and also are highly robust.
● Ease in scalability.
● The sharding property of MongoDB enables it to perform fast and in an efficient manner in
the distributed functions. This is also possible since it supports horizontal scaling of data.
● Language is highly rich in the query. MongoDB has its own query language called Mongo
query language, which can replace SQL ones. Similarly, utility functions and map or reduce
can replace complicated aggregate functions.
VEC – CSE IV Semester – II Year – 19IT205T – DBMS
● Multiple Servers: The database can run over multiple servers. Data is duplicated to
foolproof the system in the case of hardware failure.
● Auto-sharding: This process distributes data across multiple physical partitions called
shards. Due to sharding, MongoDB has an automatic load balancing feature.
● MapReduce: It supports MapReduce and flexible aggregation tools.
● Failure Handling: In MongoDB, it’s easy to cope with cases of failures. Huge numbers of
replicas give out increased protection and data availability against database downtime
like rack failures, multiple machine failures, and data center failures, or even network
partitions.
● GridFS: Without complicating your stack, any sizes of files can be stored. GridFS feature
divides files into smaller parts and stores them as separate documents.
● Schema-less Database: It is a schema-less database written in C++.
● Document-oriented Storage: It uses BSON format which is a JSON-like format.
● Procedures: MongoDB JavaScript works well as the database uses the language instead
of procedures.
Database: In simple words, it can be called the physical container for data. Each of the databases
has its own set of files on the file system with multiple databases existing on a single MongoDB
server.
Collection: A group of database documents can be called a collection. The RDBMS equivalent
to a collection is a table. The entire collection exists within a single database. There are no
schemas when it comes to collections. Inside the collection, various documents can have varied
fields, but mostly the documents within a collection are meant for the same purpose or for
serving the same end goal.
VEC – CSE IV Semester – II Year – 19IT205T – DBMS
Document: A set of key–value pairs can be designated as a document. Documents are associated
with dynamic schemas. The benefit of having dynamic schemas is that a document in a single
collection does not have to possess the same structure or fields. Also, the common fields in a
collection’s document can have varied types of data.
MongoDB RDBMS
Document-oriented and non-relational Relational database
database
Document based Row based
Field based Column based
Collection based and key–value pair Table based
Gives JavaScript client for querying Doesn’t give JavaScript for querying
Relatively easy to setup Comparatively not that easy to setup
Unaffected by SQL injection Quite vulnerable to SQL injection
Has dynamic schema and ideal for Has predefined schema and not good for
hierarchical data storage hierarchical data storage
100 times faster and horizontally scalable By increasing RAM, vertical scaling can happen
through sharding
● Text search
● Graph processing
● Global replication
● Economical
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
mysqld/Oracle mongod
mysql/sqlplus mongo
The MongoDB CRUD concept has two operations, the first one is Read Operation and the second one
is Write operation. In Read Operation we have READ operation and in the Write operation we
have CREATE, UPDATE AND DELETE operation as mentioned in the below figure.
VEC – CSE IV Semester – II Year – 19IT205T – DBMS
The below two methods are used for MongoDB Create Operations.
● db.collection_name.insertOne()
● db.collection_name.insertMany()
In the following example, we can see the Collection "users_detail" is created with the below data.
● db.collection_name.find()
The following methods are used to perform the Update Operations in MongoDB.
VEC – CSE IV Semester – II Year – 19IT205T – DBMS
● db.collection_name.updateOne()
● db.collection_name.updateMany()
● db.collection_name.replaceOne()
The following methods are used to perform the Delete Operations in MongoDB.
● db.collection.deleteOne()
● db.collection.deleteMany()