Practical-1
Aim: Introduction to NOSQL Database
a) Basic concepts of NoSQL-DB, RDBMS vs. NoSQL, Applications of NOSQL, Advantages and
disadvantages of NOSQL, CAP theorem.
Answer:
1. Basic concepts of NoSQL-DB.
NoSQL (Not Only SQL) databases are designed to handle large volumes of unstructured, semi-
structured, and structured data. Unlike relational databases (RDBMS), NoSQL databases provide
flexible schemas, scalability, and high availability.
NoSQL databases are especially useful for working with large sets of distributed data.
2. RDBMS vs. NoSQL.
RDBMS NoSQL
RDBMS application store data in the NoSQL is a non-relational database system.
form of table structure. It stores data in the form of unstructured
manner.
Schema is mandatory to store the data. Schema design is not required.
Its difficult to make changes in database Enables easy and frequent changes to
once it is defined. database.
Support ACID properties compliance. Doesn’t support ACID properties.
Open source application. Open source program.
Example: MySQL, ORACLE, SQL Example: APACHEHBASE,
Server. IBMDOMAIN.
3. Applications of NoSQL.
➢ Social media platforms.
➢ E-commerce.
➢ Big data and analytics.
➢ IoT applications.
➢ Real-time applications.
4. Advantages and Disadvantages of NoSQL.
Advantages.
➢ Flexible schema.
➢ High scalability and availability.
➢ Handles large volumes of data efficiently.
➢ Faster read/write operations.
➢ Suitable for distributed systems.
Disadvantages.
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 1
➢ Lacks standardization.
➢ Not suitable for complex transactions.
➢ Eventual consistency can cause temporary data inconsistency.
5. CAP Theorem.
The CAP theorem states that a distributed database can only provide two out of the following three
properties :
• Consistency ( C ) : Consistency means that all nodes in the network see the same data at the
same time. Consistency refers to every client having the same view of the data. Consistency
in CAP refers to sequential consistency, a very strong form of consistency.
• Availability ( A ) : Availability means that each read or write request for a data item will
either be processed successfully or will receive a message that the operation cannot be
completed. Every non-failing node returns a response for all the read and write requests in a
reasonable amount of time.
• Partition Tolerance ( P ) : Partition tolerance means that the system can continue operating
even if the network connecting the nodes has a fault that results in two or more partitions,
where the nodes in each partition can only communicate among each other. That means, the
system continues to function and upholds its consistency guarantees in spite of network
partitions. Network partitions are a fact of life.
b) Types of NOSQL database- document database, key-value database, wide-column stores, and graph
database.
Answer:
1. Document database.
A Document Data Model is a lot different than other data models because it stores data in JSON,
BSON, or XML documents. in this data model, we can move documents under one document and
apart from this, any particular elements can be indexed to run queries faster. Often documents are
stored and retrieved in such a way that it becomes close to the data objects which are used in many
applications which means very less translations are required to use data in applications. JSON is a
native language that is often used to store and query data too.
{
"Name" : "Yashodhra",
"Address" : "Near Patel Nagar",
"Email" : "yahoo123@[Link]",
"Contact" : "12345"
}
2. Key-Value database.
A key-value data model or database is also referred to as a key-value store. It is a non-relational type
of database. In this, an associative array is used as a basic database in which an individual key is
linked with just one value in a collection. For the values, keys are special identifiers. Any kind of
entity can be valued. The collection of key-value pairs stored on separate records is called key-value
databases and they do not have an already defined structure.
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 2
3. Wide-Column Stores.
A wide-column database is a type of NoSQL database in which the names and format of the columns
can vary across rows, even within the same table. Wide-column databases are also known as column
family databases. A wide-column database is a NoSQL database that organizes data storage into
flexible columns that can be spread across multiple servers or database nodes, using multi-
dimensional mapping to reference data by column, row, and timestamp.
4. Graph database.
A graph database (GDB) is a database that uses graph structures for storing data. It uses nodes,
edges, and properties instead of tables or documents to represent and store data. The edges represent
relationships between the nodes. This helps in retrieving data more easily and, in many cases, with
one operation. Graph databases are commonly referred to as NoSQL. Ex: Neo4j, Amazon Neptune,
ArangoDB etc.
• Nodes : These are the instances of data that represent objects which is to be tracked.
• Edges : As we already know edges represent relationship between nodes.
• Properties : It represents information associated with nodes.
c) Write steps for Installation of MongoDB, Mongo Compass/Studio 3T on Windows.
Answer:
To install MongoDB, MongoDB Compass, and Studio 3T on a Windows system and verify their
successful installation.
Step 1 : Download and Install MongoDB
Download msi file from below link :
[Link]
Practical-2
Aim: Implementation of CRUD (create, read, update and delete) operations using MongoDB tool.
• Perform following operations on MongoDB.
1. Create a User
[Link]({user:"nilesh",pwd:"nap",roles:[{role:"dbAdmin",db:"admin"}]})
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 3
2. Create a User with multiple roles.
[Link]({user:"nilesh1",pwd:"nap",roles:["dbAdmin","userAdmin"]})
3. Get list of all databases.
show databases *OR* show dbs
4. Create database named – customer.
use customer
5. Get list of all collections of database customer.
show collections
6. Create a collection named – customer_info.
[Link](“customer_info”)
7. Insert single document into collection – customer_info.
db.customer_info.insertOne({"acc_no":101,"name":"tapan","city":"Ahmedabad","balance":2
5000})
8. Insert multiple documents simultaneously into collection – customer_info.
db.customer_info.insertMany([{"acc_no":102,"name":"viren","city":"Vadodara","balance":2
2000},{"acc_no":104,"name":"satish","city":"Surat","balance":29000},
{"acc_no":103,"name":"aman","city":"Rajkot","balance":26000}])
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 4
9. Display all the documents of collection – customer_info.
db.customer_info.find()
10. Display (find) customer with account number 103.
db.customer_info.find({acc_no:103})
11. Display only name and city from collection – customer_info.
db.customer_info.find({},{name:1, city:1})
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 5
12. Find details of customers having account number more than 102.
db.customer_info.find({acc_no:{$gt:102}})
13. Display all the details of customer except balance.
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 6
db.customer_info.find({},{balance:0})
14. Display first document of customer_info.
db.customer_info.findOne()
15. Find first occurrence of name Satish.
db.customer_info.findOne({name:"satish"})
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 7
16. Change the city of Aman to Gandhinagar.
db.customer_info.updateOne({name:"aman"},{$set:{city:"Gandhinagar"}})
17. Updata balance to 28000 for all customer who live in city Ahmedabad.
db.customer_info.updateMany({city:"Ahmedabad"},{$set:{balance:28000}})
18. Remove the city field for Aman.
db.customer_info.updateOne({name:"aman"},{$unset:{city:"Gandhinagar"}})
19. Increment the balance of Viren by 1000.
db.customer_info.updateOne({name:"viren"},{$inc:{balance:1000}})
20. Update details of customer “Ansh”, insert the data if name not found.
db.customer_info.updateOne({name:"ansh"},{$set:{acc_no:105,"name":"ansh","city":"Anand
","balance":24000}},{upsert:true})
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 8
21. Rename field “city” to “location” where acc_no is 104.
db.customer_info.updateOne({acc_no:104},{$rename:{"city":"location"}})
22. Remove document with account number 101.
db.customer_info.deleteOne({acc_no:101})
23. elete all the documents which has city Ahmedabad.
db.customer_info.deleteMany({city:'Ahmedabad'})
24. Write other ways to display documents in organized manner.
I. db.customer_info.find().forEach(printjson)
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 9
II. db.customer_info.find().pretty()
III. db.customer_info.find({name:”viren”}).forEach(printjson)
Exercise :
Create a user of your name with every database’s admin, create a database named uvpce_enno,
create collection named “your name”, insert 10 documents into collection and compulsory fields
in every document: enno, age, city. After that, execute following operations :
1. Display all records having age 20.
[Link]({age: 20})
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 10
2. Display city and age of enno 4.
[Link]({enno: 4}, {city: 1, age: 1, _id: 0})
3. Update the age to 25 whose enno is greater than 4.
[Link]({enno: {$gt: 4}}, {$set: {age: 25}})
4. Add a new field to enno 5.
[Link]({enno: 5}, {$set: {status: "active"}})
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 11
5. Delete the document for which city is baroda.
[Link]({city: "Baroda"})
6. Rename the field enno to rollno for every document.
[Link]({}, {$rename: {"enno": "rollno"}})
7. Drop a specific collection and a specific database.
db.your_name.drop()
[Link]()
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 12
2CEIT405: NOSQL/23012011118 / Vaibhav Suthar Page 13