Module 4
Module 4
DOCUMENT DATABASES
Prepared by:
Madhuri J
Assistant Professor
Department of Computer Science and Engg
Bangalore Institute of Technology
2
}
6
{
"firstname": "Pramod",
"citiesvisited": [ "Chicago", "London", "Pune", "Bangalore" ],
"addresses": [
{ "state": "AK",
"city": "DILLINGHAM",
"type": "R"
},
{ "state": "MH",
"city": "PUNE",
"type": "R" }
],
"lastcity": "Chicago"
}
•We can see that above records are similar, but have
differences in attribute names.This is allowed in document
databases.
7
MongoDB
• Each MongoDB instance has multiple databases, and
each database can have multiple collections.
• RDBM
• When we store a document, we have to choose which
database and collection this document belongsS tables
are collections in MongoDB.
9
Collections
• A collection is a group of documents. Collections typically
store documents that have similar contents.
• Not all documents in a collection are required to have the
same fields, because document databases have a flexible
schema.
• Embedding child documents as subobjects inside
documents provides for easy access and better
performance.
11
CRUD operations
• Document databases typically have an API or query
language that allows developers to execute the CRUD
(create, read, update, and delete) operations.
• Create: Documents can be created in the database. Each
document has a unique identifier.
• Read: Documents can be read from the database. The API or
query language allows developers to query for documents using
their unique identifiers or field values. Indexes can be added to the
database in order to increase read performance.
• Update: Existing documents can be updated — either in whole or
in part.
• Delete: Documents can be deleted from the database.
12
Features
• Consistency
• Consistency in MongoDB database is configured by using the replica sets
and choosing to wait for the writes to be replicated to all the slaves or a
given number of slaves.
• Every write can specify the number of servers the write has to be
propagated to before it returns as successful.
db.runCommand({ getlasterror : 1 , w : "majority" })
db.runCommand( { getLastError: 1, w: 2, wtimeout:5000 } )
You can increase the w value for stronger consistency but you will suffer on
write performance
• 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
Mongo mongo = new Mongo("localhost:27017");
mongo.slaveOk();
This allows the current connection to allow read operations to run on secondary
nodes.
13
• Transactions
• Transactions at the single-document level are known as atomic
transactions.
• Control over the write can be achieved by using WriteConcern
parameter.
• Different levels of WriteConcern can be chosen at different safety level
during writes; for example, when writing log entries, lowest level of
safety can be used, WriteConcern.NONE.
15
• Availability
• 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.
16
• Query Features
• We can query the data inside the document without having to
retrieve the whole document by its key and then introspect the
document.
• The SQL for this would be:
SELECT * FROM order
• The equivalent query in Mongo shell would be:
db.order.find()