0% found this document useful (0 votes)
10 views17 pages

Module 4

Document databases, also known as document-oriented databases, store information in flexible documents rather than fixed rows and columns, allowing for a non-relational data model. They offer advantages such as an intuitive data model, flexible schema, and ease of horizontal scaling, making them suitable for use cases like catalogs and content management systems. Key features include CRUD operations, querying through APIs, and replication for high availability, with MongoDB being a prominent example of a document database.

Uploaded by

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

Module 4

Document databases, also known as document-oriented databases, store information in flexible documents rather than fixed rows and columns, allowing for a non-relational data model. They offer advantages such as an intuitive data model, flexible schema, and ease of horizontal scaling, making them suitable for use cases like catalogs and content management systems. Key features include CRUD operations, querying through APIs, and replication for high availability, with MongoDB being a prominent example of a document database.

Uploaded by

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

1

DOCUMENT DATABASES
Prepared by:
Madhuri J
Assistant Professor
Department of Computer Science and Engg
Bangalore Institute of Technology
2

• A document database (also known as a document-


oriented database or a document store) is a database that
stores information in documents.
• Document databases are considered to be non-relational
(or NoSQL) databases. Instead of storing data in fixed
rows and columns, document databases use flexible
documents.
• The database stores and retrieves documents, which can
be XML, JSON, BSON, and so on.
• The documents stored are similar to each other but do not
have to be exactly the same.
3
4

Document databases offer a variety of advantages,


including:
• An intuitive data model that is fast and easy for developers to work
with.
• A flexible schema that allows for the data model to evolve as
application needs change.
• The ability to horizontally scale out.
•The document model works well with use cases such as
catalogs, user profiles, and content management systems
where each document is unique and evolves over time.
5

• Consider a row in a traditional RDBMS.


{
"firstname": "Martin",
"likes": [ "Biking",
"Photography" ],
"lastcity": "Boston",
"lastVisited":

}
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

Key features of document


databases
• Document model
• Flexible schema
• Distributed and resilient
• Querying through an API or query language
• Developers don't have to worry about manually splitting
related data across multiple tables when storing it or
joining it back together when retrieving it.
8

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

What are documents?


• A document is a record in a document database. A
document typically stores information about one object
and any of its related metadata.
• Documents store data in field-value pairs. The values can
be a variety of types and structures, including strings,
numbers, dates, arrays, or objects.
10

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

• Number of writes can be synced to disk or to propagate to two or


more slaves. This is known as WriteConcern:
• Certain writes are written to the master and some slaves by setting
WriteConcern to REPLICAS_SAFE.
DBCollection shopping = database.getCollection("shopping");
shopping.setWriteConcern(REPLICAS_SAFE);
• WriteConcern can also be set per operation by specifying it on the
save command:
WriteResult result = shopping.insert(order, REPLICAS_SAFE);
14

• 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

• Replica sets are generally used for data redundancy, automated


failover, read scaling, server maintenance without downtime, and
disaster recovery. Similar availability setups can be achieved with
CouchDB, RavenDB, Terrastore, and other products.
17

• 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()

You might also like