0% found this document useful (0 votes)
38 views

noSQL Module-4 (Sindhu)

Yy66yyubhh ghh

Uploaded by

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

noSQL Module-4 (Sindhu)

Yy66yyubhh ghh

Uploaded by

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

MODULE 4

DOCUMENT DATABASES

SINDHU RANI K R
(1VJ21CS041)
4.1 What are Document Databases?

A document database is a type of NoSQL database (like MongoDB) that stores documents instead of rows in a table
(like in traditional relational databases). This data is represented in formats like JSON, XML, or BSON.

➢ Key-Value Store: a key is a unique identifier for the document, and the value is the document itself, which can
contain various types of data.

➢ Hierarchical: A document can contain other documents or nested lists (arrays). So data will be more complex and
organized in a tree-like structure.

➢ Flexible Structure: Documents in the same collection can have different fields or attributes. You don't have to follow
a rigid structure like you do with relational databases.
DOCUMENT DATABASES:

In a document database, documents can have different fields, even if they are in the same collection.
For example, one document might have a "likes" field (e.g., hobbies), while another document might not.
Similarly, one document could have an "addresses" field.
This flexibility allows documents to have different attributes, making it easier to store diverse or changing
data.
1. First Document: 2. Second Document:
{
{ "firstname": "Pramod",
"firstname": "Martin", "citiesvisited": ["Chicago", "London", "Pune", "Bangalore"],
"likes": ["Biking", "Photography"], "addresses": [
"lastcity": "Boston", { "state": "AK", "city": "Dillingham", "type": "R" },
"lastVisited": "2024-01-01" { "state": "MH", "city": "Pune", "type": "R" }
} ],
"lastcity": "Chicago"
}
4.2 FEATURES:
To understand the features of Document database we are comparing RDBMS with
MongoDB(noSQL)
▪ Instance in MongoDB = Database Server in RDBMS(In both, you store data).
▪ Database in MongoDB = Schema in RDBMS (Group of data).
▪ Collection in MongoDB = Table in RDBMS (Where data is stored).
▪ Document in MongoDB = Row in RDBMS (A single piece of data).

Example:

•db: database.
•customers: collection.
•{ name: "John", age: 30 }: document (like a row in RDBMS).

So, in simple terms:MongoDB = Flexible way to store data. choose a database, pick a
collection, and add a document.
4.2.1 CONSISTENCY:

Consistency in MongoDB means making sure that the data you write to the database is saved correctly across
all the servers that are part of your MongoDB system. by this we can trust that the data is same everywhere, even if
some servers are slower or have issues.
In MongoDB, a replica set is a group of servers that all store copies of the same data. One server is the primary
(main server), and the others are secondary (backup servers).
When you write data, you can decide how many servers need to confirm the write before MongoDB says it's
successful:
1. If you set w: "majority", MongoDB needs the primary server and at least one backup server to confirm the
write before it’s successful.
2. If you set w: 1, only the primary server needs to confirm the write.\
CONSISTENCY:

slaveOk for Reading from Secondary Servers


MongoDB allows to read data from secondary servers (slaves) to speed up read operations.
However, the data on the secondary might be slightly outdated because it may not have the latest changes from the primary server.
To enable reading from secondaries, you use the slaveOk() setting.

This command tells MongoDB that you are okay with reading data from secondary servers.
Example with a query:

This allows the find() operation to run on a secondary server instead of the primary.
4.2.2 TRANSACTIONS:

MongoDB doesn’t support multi-document transactions like in RDBMS. In MongoDB, you can only have atomic
transactions at the single-document level, meaning MongoDB guarantees that changes to a single document are either
fully applied or not applied at all.

Write Concern and Transactions: MongoDB doesn’t support full multi-document transactions, but you can control how
many servers should confirm a write before it’s successful using WriteConcern. This ensures that writes are safely stored
on the primary and secondary servers.

For example:

This ensures that the write is confirmed by the primary server and at least one secondary server before the operation is
considered successful.
TRANSACTIONS:

We can also set the WriteConcern for each operation.


For example, when inserting an order:

If the write doesn’t succeed on enough servers, MongoDB throws an exception. You can catch this exception and handle
it, like so:
THANK YOU

You might also like