Module-3
Module-3
OUTLINE
MongoDB
CouchDB
Terrastore
OrientDB
RavenDB
Lotus Notes
KEY FEATURES OF DOCUMENT
DATABASES
Schema Flexibility – No predefined structure; each
document can have different fields.
Hierarchical Data Storage – Supports nested objects and
arrays within documents.
Rich Querying – Allows filtering, indexing, full-text search,
and aggregation.
Horizontal Scalability – Uses sharding to distribute data
across multiple servers.
High Performance – Optimized for fast reads and writes,
making them ideal for real-time applications.
DIFFERENCE BETWEEN KEY-VALUE AND
DOCUMENT DATABASE
Features Document Database Key-Value Store
Data Format Stores data as structured JSON, Stores data as simple key-
BSON, or XML documents value pairs
Schema each document can have different fields values can be anything: text,
JSON, binary, etc.
Query Language NoSQL query languages (e.g., Only supports simple
MongoDB Query Language) GET/PUT operations
Performance Fast for reads/writes but slower than High-speed performance for
key-value stores key-based retrieval
E-commerce catalog (structured Caching user sessions,
Use Case Example
product details), social media posts, session storage,
IoT data leaderboards
USE CASES OF DOCUMENT DATABASES
E-Commerce – Storing dynamic product catalogs with
varying attributes.
Content Management Systems – Managing blog posts,
comments, and metadata.
Real-Time Chat Applications – Storing chat history and
user messages.
IoT Data Storage – Managing large-scale sensor data from
connected devices.
Personalization & Recommendations – Storing user
preferences and behavior data.
MONGO DB FEATURES:
MONGO DB FEATURES
Ad-hoc Queries
Generally, when we design a schema of a database, we don’t
know in advance about the queries we will perform.
Ad-hoc queries are the queries not known while structuring
the database.
So, MongoDB provides ad-hoc query support which makes it so
special in this case.
Ad-hoc queries are updated in real time, leading to an
improvement in performance.
AD-HOC QUERIES
Adhoc Queries SET @SQL =
SELECT LastName, N'SELECT LastName,
FirstName FirstName
FROM Person.Person;
FROM Person.Person
WHERE BusinessEntityID
Stored
Procedure(dynamic = @ID';
query) SET @ID = 1;
DECLARE @SQL SET @Param = N'@ID INT ';
NVARCHAR(MAX);
EXEC sp_executesql @SQL,
DECLARE @ID INT; @Param, @ID = @ID;
DECLARE@Param
NVARCHAR(MAX);
ADHOC QUERIES IN MONGODB
const results = await db.collection.find({ name:
req.query.name });
Database
Database is a physical container for collections.
A single MongoDB server typically has multiple databases.
Collection
Collection 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.
Typically, all documents in a collection are of similar or related
purpose.
Document
A document is a set of key-value pairs.
Documents 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.
MONGODB - DATATYPES
String − This is the most commonly used datatype to
store the data. String in MongoDB must be UTF-8 valid.
Integer − This type is used to store a numerical value.
Integer can be 32 bit or 64 bit depending upon your
server.
Boolean − This type is used to store a boolean (true/
false) value.
Double − This type is used to store floating point values.
Arrays − This type is used to store arrays or list or
multiple values into one key.
Timestamp − used to store a timestamp. This can be
handy for recording when a document has been modified
or added.
Object − This datatype is used for embedded documents.
Null − This type is used to store a Null value.
Symbol − This datatype is used identically to a string; however,
it's generally reserved for languages that use a specific symbol
type.
Date − This datatype is used to store the current date or time in
UNIX time format. You can specify your own date time by
creating object of Date and passing day, month, year into it.
Object ID − This datatype is used to store the document’s ID.
Binary data − This datatype is used to store binary data.
Code − This datatype is used to store JavaScript code into the
document.
Regular expression − This datatype is used to store regular
expression.
MONGODB COMMANDS
Command to Start MongoDB
sudo service mongodb start
The find() function provides the easiest way to retrieve data from
multiple documents within one of your collections.
> db.media.find( { "Author" : "Membrey, Peter" } )
You can use the limit() function to specify the maximum number
of results returned.
The following example returns only the first ten items in your
media collection:
> db.media.find().limit(10)
The following example skips the first twenty documents in your
media collection:
> db.media.find().skip(20)
USING THE SORT, LIMIT, AND SKIP
FUNCTIONS
You can use the sort function to sort the results returned
from a query.
You can sort the results in ascending or descending order
using 1 or -1, respectively.
The function itself is analogous to the ORDER BY
statement in SQL, and it uses the key’s name and sorting
method as criteria, as in this example:
> db.media.find().sort( { Title: 1 })
This will sort the results based on the Title key’s value in
ascending order.
This is the default sorting order when no parameters are
specified.
You would add the -1 flag to sort in descending order.
RETRIEVING A SINGLE DOCUMENT
If you want to receive only one result, however, querying for all
documents —which is what you generally do when executing a
find() function —would be a waste of CPU time and memory.
For this case, you can use the findOne() function to retrieve a
single item from your collection.
Overall, the result is identical to what occurs when you append
the limit(1) function.
The syntax of the findOne() function is identical to the syntax of
the find() function:
> db.media.findOne()
It’s generally advised that you use the findOne() function if you
expect only one result.
count() function returns the number of documents in
specified collection.
>db.media.count() 2
Renaming a Collection
Obviously, it might happen that you have a collection that
you named incorrectly, but you’ve already inserted some
data into it.
This might make it troublesome to remove and read the
data again from scratch.
Instead, you can use the renameCollection() function to
rename your existing collection
> db.media.renameCollection("newname") { "ok" : 1 }
insert() function is used for inserting and update() is
used for modifying a document, remove() is used to
remove a document.
To remove a single document from your collection,
you need to specify the criteria you’ll use to find the
document.
db.newname.remove( { "Title" : "Different Title" } )
you can use the following snippet to remove all
documents from the newname library:
> db.newname.remove({ })
TRANSACTIONS
Transactions, in the traditional RDBMS, mean that you can start
modifying the database with insert, update, or delete commands
over different tables and then decide if you want to keep the
changes or not by using commit or rollback.
These constructs are generally not available in NoSQL solutions
—a write either succeeds or fails.
Transactions at the single-document level are known as atomic
transactions.
Transactions involving more than one operation are not possible,
although there are products such as RavenDB that do support
transactions across multiple operations.
A finer control over the write can be achieved by using
WriteConcern parameter.
AVAILABILITY
The CAP theorem dictates that we can have only two of
Consistency, Availability, and Partition Tolerance.
Document databases try to improve on availability by replicating
data using the master-slave setup.
The same data is available on multiple nodes and the clients can
get to the data even when the primary node is down.
MongoDB implements replication, providing high availability
using replica sets.
In a replica set, there are two or more nodes participating in an
asynchronous master-slave replication.
The replica-set nodes elect the master, or primary, among
themselves.
Assuming all the nodes have equal voting rights; users can
assign a priority —a number between 0 and 1000 —to a node.
All requests go to the master node, and the data is replicated to
the slave nodes.
If the master node goes down, the remaining nodes in the
replica set vote among themselves to elect a new master; all
future requests are routed to the new master, and the slave
nodes start getting data from the new master
When the node that failed comes back online, it joins in as a
slave and catches up with the rest of the nodes by pulling all the
data it needs to get current.
• We have two nodes, mongo A and mongo B, running the MongoDB
database in the primary data-center, and mongo C in the secondary
datacenter.
• If we want nodes in the primary datacenter to be elected as primary
nodes, we can assign them a higher priority than the other nodes.
When the primary node goes down, the driver talks to the new
primary elected by the replica set.
Replica sets are generally used for
– Data redundancy
– Automated failover
– Read scaling
– Server maintenance without downtime
– Disaster recovery.
4. E-Commerce Applications
E-commerce applications often need to have flexible schema for
products and orders, as well as the ability to evolve their data
models without expensive data migration
WHEN NOT TO USE
1. Complex Transactions Spanning Different Operations
If you need to have atomic cross-document operations, then
document databases may not be for you.
However, there are some document databases that do support
these kinds of operations, such as RavenDB.