You are on page 1of 6

TUNIS BUSINESS SCHOOL

UNIVERSITY OF TUNIS

LAB 3: NoSQL Database

Exercise 1:
A video store owner has a relational database to store information about: Film,
actors, producer. The relational schema is as below:

Film (idfilm, title, category, country, year,#iddirector, #idactor)

Actor(idactor, fname, Lname)

Play(#idfilm,#idActor)

Director(iddirector, fname, Lname, date_birth)

The owner wants to design a new No SQL data base:

1. Propose an adequate structure of NoSQL data base.


Document Film

{
"id":123
"title": "Taxi driver",
"category": "drama",
"country": "USA",
"year": 1976,
"director": {
"id":"D123",
"last_name": "Scorcese",
first_name: "Martin",
"birth_date": "1962"
},
"actors": [
{
"id":"A154",
"first_name": "Jodie",
"last_name": "Foster",
"birth_date": null,
}
{
"id":"A155",
first_name: "Robert",
"last_name": "De Niro",
"birth_date": "1943",
"role": "Travis Bickle ",
}
]
}
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

2. Use Mongodb to create Video_store collection and its document(s)


based on the structure that you proposed in question 1.
db.createCollection (“video_store”)

db.video_store.insert(
[{
id:123,
title: "Taxi driver",
category: "drama",
country: "USA",
year: 1976,
director: {
id:"D123",
last_name: "Scorcese",
first_name: "Martin",
birth_date: "1962"},
actors: [
{
id:"A154",
first_name: "Jodie",
last_name: "Foster",
birth_date: null},
{
id:"A155",
first_name: "Robert",
last_name: "De Niro",
birth_date: "1943",
role: "Travis Bickle"}
]
}
]
)
3. Insert suitable information (two ways).
1st way

db.video_store.insertOne({

2nd way

db. video_store.createIndex(

4. Display all inserted documents


db. video_store.find() ----or----

db. video_store.find().pretty()

5. Display all films that belong to Romantic gender.


db. video_store.find( {category: "Romantic"} )

6. Display all films that appear in 2013.


TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

db. video_store.find( {Year: 2013} )

7. Add a new extra field length.


We could update the inserted documents and add the new field length with $set.

db. video_store.updateMany({name: “”}, $set{length: 120})

Or add a new document with the new field length.

8. Display all film of the Director ‘Steven Spielberg’.


db. video_store. find( {$and : [ {director.first_name: " Steven ",
director.last_name: " Spielberg "} ]} )

9. Rename the fiels catagory to Type.


db. video_store.updateMany({},{$rename:{category:"Type"}})

10. If we want to add a new document with a new field Producer.


Write the adequate query.
add a new document with the new field

Exercise 2:
We have a collection called “Product”.

The documents in the Product collection contain:

 Product id
 Product name
 Product category
 Quantity
 Price

The product category is a list as below:

 Cosmetic
 Hygiene
 Textile
 …
1. Propose an adequate structure of NoSQL data base.
2. Use Mongodb to create Product collection and its document(s)
based on the structure that you proposed in question 1.
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

db.Products.insertMany ([

{id:100,name:"milk",category:"food",quantity:150,price:"1.500"},

{id:123,name:"l'oreal",category:"Hygiene",quantity:100,price:"10.25
0"},

{id:453,name:"pant",category:"textile",quantity:1000,price:"35.900"
},

{id:284,name:"tshirt",category:"textile",quantity:850,price:"28,950"
},

{id:145,name:"Sun Screen", category:"Cosmetic", quantity:550,


price:"58,750"}

])

3. Insert suitable information (two ways).

4. Write a query to display information that belongs to a specific


product “Sun Screen”.
db. Product.find( {name: " Sun Screen "} )

5. Write a query to display information that belongs to a specific


product id=123.
db. Product.find( {id: 123} ).pretty()

“pretty” method to make the document seem more appealing.

6. Write a query to increment the field quantity by 10 of product


“Sun Screen”
db. Product.update({name: “Sun Screen”}, {$inc:{quantity: 10}})

7. Write a query to display information that belongs to product


with quantity greater than 40.
db. Product.find({quantity: {$gt:40}})

8. Write a query to display information that belongs to product


name starting with letter “S” with quantity less than 40. (two
ways)
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

1st way

db. Product. find({{name :{/^ S .*/i }, quantity: {$lt:40}} )

2nd way

db.Product.find({$and:[{name:/^S.*/i},{quantity:{$lt:40}}]})

9. Write a query to display information that belongs to product


“sun screen” or product with a quantity more than 25.
db.Products.find({$or:[{name :/^S.*/i},{quantity:{$lt:25}}]})

10. Write a query to display the total quantity of all products


with category “Cosmetic”.
db. Product. aggregate([{$match:{category:"Cosmetic"}},{$group:
{_id:"category",total: {$sum: "$quantity"}}}])

11. Write a query to display the total quantity of all products in


each category.
db. Product. aggregate([{$group:{_id:"$category",total:{$sum:
"$quantity"}}}])

12. Write a query to display the average price of all products in


each category having quantity greater than 25.
db.Product.aggregate( [

// Stage 1: Filter product documents by quantity

{$match: {"quantity”: { $gt: 25 }}},

// Stage 2: Group remaining documents by category and calculate results

{$group: { _id: "$category", totalOrderValue: { $sum: { $multiply:


[“$price","$quantity" ] } }, averageOrderQuantity: { $avg: "$quantity" } }}] )

13. Write a query to display the average quantity of all products in


each category in descending order.
db.Product.aggregate( [

// Stage 1: Group documents by category and calculate results

{$group: { _id: "$category", totalOrderValue: { $sum: { $multiply:


[“$price","$quantity" ] } }, averageOrderQuantity: { $avg: "$quantity" } }},
TUNIS BUSINESS SCHOOL
UNIVERSITY OF TUNIS

// Stage 2: Sort documents by totalOrderValue in descending order

{ $sort: { averageOrderQuantity: -1 } }] )

You might also like