Module 4
Module 4
MODULE -4
DOCUMENT DATABASES
Department of
Computer Science & Engineering
www.cambridge.edu.in
DOCUMENT DATABASES
Documents are the main concept in document databases.
The database stores and retrieves documents, which can be XML, JSON,
BSON(Binary Javascript Object Notation), and so on.
The _id is a special field that is found on all documents in Mongo, just like ROWID in
Oracle. In MongoDB, _id can be assigned by the user, as long as it is unique.
{
"firstname": "Martin",
"likes": [ "Biking", "Photography" ],
"lastcity": "Boston",
"lastVisited":
}
Every write can specify the number of servers the write has to
be propagated to before it returns as successful.
A command like
Replica sets also allow you to increase the read performance by allowing
reading from slaves by setting slaveOk; this parameter can be set on the
connection, or database, or collection, or individually for each operation
Scaling for heavy-read loads can be achieved by adding more read slaves, so
that all the reads can be directed to the slaves.
Given a heavy-read application, with our 3-node replica-set cluster, we can
add more read capacity to the cluster as the read load increases just by
adding more slave nodes to the replica set to execute reads with the slaveOk
flag ( Figure 9.2). This is horizontal scaling for reads.
Once the new node, mongo D, is started, it needs to be added to the replica
set.
rs.add("mongod:27017");
When a new node is added, it will sync up with the existing nodes, join the
replica set as secondary node, and start serving read requests.
An advantage of this setup is that we do not have to restart any other nodes,
and there is no downtime for the application either
When we want to scale for write
Splitting the data on the first name of the customer ensures that the
data is balanced across the shards for optimal write performance;
furthermore, each shard can be a replica set ensuring better read
performance within the shard
Suitable Use Cases
9.3.1. Event Logging Applications have different event logging needs; within
the enterprise, there are many different applications that want to log events.
Document databases can store all these different types of events and can act
as a central data store for event storage. This is especially true when the type
of data being captured by the events keeps changing. Events can be sharded
by the name of the application where the event originated or by the type of
event such as order_processed or customer_logged.
9.3.2. Content Management Systems, Blogging Platforms Since
document databases have no predefined schemas and usually
understand JSON documents, they work well in content
management systems or applications for publishing websites,
managing user comments, user registrations, profiles, web-facing
documents.
E-Commerce Applications E-commerce applications often need
to have flexible schema for products and orders
When Not to Use