Module 3 Mongodb
Module 3 Mongodb
MongoDB
WHAT IS MONGODB?
MongoDB is Cross-platform Open source Non-relational Distribute NoSQL Dacument-oriented
data store.
WHY MONGODB?
Few of the major challenges with traditional RDBMS are dealing with large volumes of data,
rich variety data — particularly unstructured data, and meeting up to the scale needs of enterprise
data. The need is for database that can scale out or scale horizontally to meet the scale
requirements, has flexibility with respect schema, is fault tolerant, is consistent and partition
tolerant, and can be easily distributed over a multitude of nodes in a cluster.
JSON is extremely expressive. MongoDB actually does not use JSON but BSON is Binary
JSON. It is an open standard. It is used to store complex data structures.
Features of MongoDB
Document-oriented storage
High performance
Flexible schema
High availability through replication
Horizontal scalability via sharding
Rich query language
In-built aggregation framework
Index support
MongoDB has extensive support for dynamic queries. we have static data
and dynamic queries.
Storing Binary Data
mongogoDB provides GridFS to support the storage of binary data. It can
store up to 4 MB of data.
This suffices for photographs (such as a profile picture) or small audio
clips, However, if one wishes to store movie clips, MongoDB has another
solution it stores the metadata (data about data along with the context
information) in a collection called “file”.
It then breaks the data into small pieces called chunks and stores it in the
“chunks” collection. This process make easy scalability.
Replication
It provides data redundancy and high availability. It helps to recover from
hardware failure and service interruptions.
In MongoDB, the replica set has a single primary and several secondaries.
Each write request from the client is directed to the primary. The primary
logs all write requests into its Oplog (operations log).
The Oplog is then used by the secondary replica members to synchronize
their Data.
This way there is strict adherence to consistency. Refer Figure. The
clients usually read from the primary. However, the client can also
specify a read preference that will then direct the read operations to
the secondary.
The process of SHARDING in mongodb.
Sharding is akin to horizontal scaling. It means that the large dataset is divided
and distributed over multiple servers or shards. Each shard is an independent
database and collectively they would constitute a logics database.
The prime advantages of sharding are as follows:
1. Sharding reduces the amount of data that each shard needs to store and
manage. For example, if
dataset was 1 TB in size and we were to distribute this over four shards, each
shard would house just 256 GB data. Refer Figure 6.4. As the cluster grows, the
amount of data that each shard will store and manage will decrease.
2. Sharding reduces the number of operations that each shard handles. For
example, if we were to insert data, the application needs to access only that shard
which houses that data.
MongoDB vs RDBMS
Collection vs Table
Document vs Row
Field vs Column
Embedded Document vs Join
Dynamic Schema vs Fixed Schema
Create Database
The syntax for creating database is as follows:
use DATABASE Name
To create a database by the name “myDB” the syntax is
use myDB;
switched to db myDB;
To confirm the existence of your database, type the command at the MongoDB shell:
db;
myDB
To get a list of all databases, type the below command:
> show dbs;
admin (empty)
flocal 0.078GB
test 0.078cB
>
Notice that the newly created database, “myDB” does not show up in the list above. The reason
is that
database needs to have at least one document to show up in the list
The default database in MongoDB is test. If one does not create any database, all collections are
by
stored in the test database.
Drop Database
The syntax to drop database is as follows:
db.dropDatabase();
‘To drop the database, “myDB”, first ensure that you are currently placed in “myDB” database
and then
the db.dropDatabase() command to drop the database.
use myDB;
db.dropDarabase();
CRUD Operations
CRUD stands for the four basic operations that can be performed on data in a database:
db.students.find()
system. indexes
peta: is
Save() Method
‘We cow explain the save() method. The save() method will
insert a new document if the document with specified _id does
not exist. However, if a document with the specified id exists, it
replaces the existing document with the new one.
Example : db.collection.save({
_id: ObjectId("60f6b2d4c2a1d2d1e4f8f123"),
name: "Raj",
age: 28
})
Act:
db.Students.count()
Outcome:
5
Act:
db.Students.count({Grade:"VII"});
Outcome:
4
Outcome:
PRINT FIRST THREE DOCUMENTS IN THE LIST