Understanding Nosql Basics Using Mongo DB
Understanding Nosql Basics Using Mongo DB
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
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()
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
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
$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()
where the remainder is 1000 when divided by 3000 in the inventory collection.
db.inventory.find({"name": {$regex: '.Packed.'}}).pretty()
Operator Description
Matches arrays that contain all the specified values in the
$all query condition.
16
Array Operator Examples
17
Comment Operator