0% found this document useful (0 votes)
64 views

Understanding Nosql Basics Using Mongo DB

The document provides an overview of MongoDB storage concepts including databases, collections, and documents. It then discusses key MongoDB features like being non-SQL, using BSON format, and supporting horizontal scaling. It also covers MongoDB replication using replica sets, sharding architecture and components, and various query operators for comparisons, logical operations, evaluating elements and arrays, and adding comments.

Uploaded by

apoorva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Understanding Nosql Basics Using Mongo DB

The document provides an overview of MongoDB storage concepts including databases, collections, and documents. It then discusses key MongoDB features like being non-SQL, using BSON format, and supporting horizontal scaling. It also covers MongoDB replication using replica sets, sharding architecture and components, and various query operators for comparisons, logical operations, evaluating elements and arrays, and adding comments.

Uploaded by

apoorva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Understanding NoSQL Basics Using Mongo DB

Introduction to MongoDB Storage


Database:  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.

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
document can have varied types of data.
Introduction to MongoDB Storage

MongoDB is a document-based NoSQL database. It requires no


fixed schema definition. Mongo DB stores data as BSON.
 It supports horizontal scaling. Several servers instance group
together as a cluster to support Mongo DB as a distributed
system.
MongoDB uses MongoDB query language, Replication, and
Sharding.
Replication of MongoDB servers
Replication serves as a very important feature in order to protect data loss from
one server and increase the availability of data, and give protection from failures.
MongoDB achieves replication using the concept of replica sets.It is a group of
mongod instances that host the same data set. One of the nodes is selected as the
primary or main node. All others are called secondary nodes. The primary node
receives all the operations from the user and the secondaries are updated from
the primary one by using the same operation to maintain consistency. If the
primary node goes down, one of the secondary nodes is selected as the primary
node and the operations are carried forward. When the fallen node recovers, it
joins the cluster as the secondary nodes.
Mongo clusters are created based on the idea of horizontal scaling and adding of
instances.
Storage/Sharding Architecture of Mongo DB
Sharding in Mongo DB
Sharding is used by MongoDB to store data across multiple machines. It uses
horizontal scaling to add more machines to distribute data and operation with
respect to the growth of load and demand.

1. Shards or replica sets: Each shard serves as a separate replica set. They store all the data.
Components of Sharding
They target to increase the consistency and availability of the data.
2. Configuration Servers: They are like the managers of the clusters. These servers contain
the cluster's metadata. They actually have the mapping of the cluster's data to the shards.
When a query comes, query routers use these mappings from the config servers to target
the required shard.
3. Mongo Clients: The query router is mongo instances which serve as interfaces for user
applications. They take in the user queries from the applications and serve the applications
with the required results. Usually, there are multiple query router per cluster for load
distribution.
Mongo DB Operators

MongoDB offers the following query operator types:


1. Comparison
2. Logical
3. Element
4. Evaluation
5. Array
6. Comments

7
Comparison Operators
MongoDB comparison operators can be used to compare values in a document. 

Operator Description
$eq Matches values that are equal to the given value.
$gt Matches if values are greater than the given value.
$lt Matches if values are less than the given value.
$gte Matches if values are greater or equal to the given value.
$lte Matches if values are less or equal to the given value.
$in Matches any of the values in an array.
$ne Matches values that are not equal to the given value.
$nin Matches none of the values specified in an array.
8
Examples of Comparison Operator 9
 db.inventory.find({"_id": { $eq: "LS0009100"}}).pretty()

 db.inventory.find({"quantity": { $gt: 5000}}).pretty()

 db.inventory.find({"quantity": { $lt: 5000}}).pretty()

 db.inventory.find({"quantity": { $gte: 12000}}).pretty()

 db.inventory.find({"quantity": { $lte: 1000}}).pretty()

 db.inventory.find({"price": { $in: [3, 6]}}).pretty()

 db.inventory.find({"price": { $nin: [5.23, 3, 6, 3.59, 4.95]}}).pretty()

 db.inventory.find({"price": { $ne: 5.23}})


Logical Operators
10

MongoDB logical operators can be used to filter data based on given


conditions.
These operators provide a way to combine multiple conditions.
Each operator equates the given condition to a true or false value.
Operator Description

Joins two or more queries with a logical AND and returns the documents that
$and match all the conditions.

$or Join two or more queries with a logical OR and return the documents that match
either query.
The opposite of the OR operator. The logical NOR operator will join two or more
$nor
queries and return documents that do not match the given query conditions.

$not Returns the documents that do not match the given query expression.
Logical Operator Example

 db.employees.find({ $and: [{"job_role": "Store Associate"}, {"emp_age": {$gte:

20, $lte: 30}}]}).pretty()


 db.employees.find({ $or: [{"job_role": "Senior Cashier"}, {"job_role": "Store

Manager"}]}).pretty()
 db.employees.find({ $nor: [{"job_role": "Senior Cashier"}, {"job_role": "Store

Manager"}]}).pretty()
 db.employees.find({ "emp_age": { $not: { $gte: 40}}})
11
Element Operators

The element query operators are used to identify documents using the fields
of the document.

Operator Description
$exists Matches documents that have the specified field.
Matches documents according to the specified field type. These field
$type types are specified BSON types and can be defined either by type
number or alias.

12
Element Operator Examples
db.employees.find({ "emp_age": { $exists: true, $gte:
30}}).pretty()
db.employees.find({ "address": { $exists: true}}).pretty()
db.employees.find({ "emp_age": { $type: "double"}})
db.employees.find({ "emp_age": { $type: "bool"}})

13
Evaluation Operators

The MongoDB evaluation operators can evaluate the overall data


structure or individual field in a document
Operator Description
$jsonSchema Validate the document according to the given JSON schema.

$mod Matches documents where a given field’s value is equal to the remainder
after being divided by a specified value.
$regex Select documents that match the given regular expression.

$text Perform a text search on the indicated field. The search can only be
performed if the field is indexed with a text index.
$where Matches documents that satisfy a JavaScript expression.
14
Evaluation Operator Example
 db.promo.find({ $jsonSchema: promoschema }).pretty()

 db.inventory.find({"quantity": {$mod: [3000, 1000]}}).pretty() :Find documents

where the remainder is 1000 when divided by 3000 in the inventory collection.
 db.inventory.find({"name": {$regex: '.Packed.'}}).pretty()

 db.payments.find({ $where: function() { var value = isString(this._id) &&

hex_md5(this._id) == '57fee1331906c3a8f0fa583d37ebbea9'; return value; }}).pretty()

 db.inventory.find({ $text: { $search: "Non-Fat"}}).pretty()

 db.inventory.createIndex({ "name": "text"})


15
Array Operators

Operator Description
Matches arrays that contain all the specified values in the
$all query condition.

$size Matches the documents if the array size is equal to the


specified size in a query.

$elemMatch Matches documents that match specified $elemMatch


conditions within each array element.

16
Array Operator Examples

db.inventory.find({ "category": { $all: ["healthy", "organic"]}}).pretty()


db.inventory.find({ "category": { $size: 2}}).pretty()
db.promo.find({ "daily_sales": { $elemMatch: {$gt: 100, $lt:
200}}}).pretty()

17
Comment Operator

The MongoDB comment query operator associates a comment to


any expression taking a query predicate. Adding comments to
queries enables database administrators to trace and interpret
MongoDB logs using the comments easily.
Examplemples
 db.promo.find({ "period": { $eq: 7}, $comment: "Find Weeklong
Promos"}).pretty()
 db.adminCommand( { getLog:'global'} ).log.forEach(x => {print(x)})
18

You might also like