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

Mongodb and Neo4j Practicals

The document describes modeling university data as a graph and document database. It defines nodes/labels for colleges, courses, professors and their relationships. It provides Cypher queries to create nodes, relationships and answer queries for the graph model. For the document model, it creates a "collegeee" collection and inserts sample college documents with courses and professors arrays. It then lists queries to find colleges by course, professors by age, colleges by professor count and colleges by course count.

Uploaded by

Abhishek Chavan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views

Mongodb and Neo4j Practicals

The document describes modeling university data as a graph and document database. It defines nodes/labels for colleges, courses, professors and their relationships. It provides Cypher queries to create nodes, relationships and answer queries for the graph model. For the document model, it creates a "collegeee" collection and inserts sample college documents with courses and professors arrays. It then lists queries to find colleges by course, professors by age, colleges by professor count and colleges by course count.

Uploaded by

Abhishek Chavan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Q1) Model the following Society relations between people working in

“HCL”, as a graph model, and answer the queries using Cypher.


A person can be a friend of another person. A person may have
siblings (brothers / sisters) , A person may be a
parent(mother/father) of another person. A person stays either in
Pune or Mumbai or Kolhapur. A person may be working on either
‘Finance’ or ‘Inventory’ or ‘Sales’ projects.

Solution : According to the question consider three nodes with labels


1. person(label)
2. place(label)
3. project(label)
To create node with the label and properties :
create (ajay:person{name:"ajay",place : "pune",project:"finance",age:45})
return ajay

Explanation : we can crete the node with keyword CREATE. here ajay is a node
name with label person and having the properties like name,place, project and
age and i am going to return node ajay with RETURN keyword.
like wise i have created some other nodes

create (anil:person{name:"anil",place : "mumbai",project:"inventory",age:25})


return anil
create (akash:person{name:"akash",place : "kolhapur",project:"sales",age:35})
return akash
create (amar:person{name:"amar",place : "pune",project:"sales",age:35}) return
amar
create (alankar:person{name:"alankar",place : "pune",project:"sales",age:35})
return alankar
create (sunil:person{name:"sunil",place : "mumbai",project:"sales",age:22})
return sunil
create (sujit:person{name:"sujit",place : "mumbai",project:"finance",age:55})
return sujit
create (soham:person{name:"soham",place
:"mumbai",project:"finance",age:65}) return soham
To See the Graph of all nodes which we have created using the MATCH and
RETURN keyword

match (n) return (n)


Like wise we are going to create other nodes with label project

create (finance:project{name:"finance"}) return finance


create (inventory:project{name:"inventory"}) return inventory
create (sales:project{name:"sales"}) return sales

match (n) return (n)

Like wise we are going to create other nodes with label place

create (pune:place{name:"pune"}) return pune


create (mumbai:place{name:"mumbai"}) return mumbai
create (kolhapur:place{name:"kolhapur"}) return kolhapur

match (n) return (n)


Now we have to crete the relation between the nodes that we have created
match(a:person),(b:project) where a.name="soham" and b.name="inventory"
create (a)-[r:works_in]-> (b) return a,b,r
here a,b is the variable name and using the create (a)-[r:works_in]-> (b) we are
goin to create the relation between nodes soham and inventory and then to see
the result use the RETURN
like wise
match(a:person),(c:place) where a.name="soham" and c.name="pune" create
(a)-[r:lives_in]-> (c) return a,c,r
create relation between all the nodes like this only.
Queries:
1. to return all the nodes having label person
match (p:person) return (p)

2. List the names of people who are parents.


match (p1:person)-[r:parent_of]-> (p2:person) return p1,p2,r

3. List the names of people working on ‘Finance‘ project


match (p:person ) where p.project="finance" return p

4. List the names of people having most friends.


match (p1:person)-[r:friend_of]-> (p2:person) return p1,p2,r

Q2) Model the following Dairy Brand information as a graph model , and
answer the following queries using Cypher. There are various dairy brands
like Amul, Go, Britannia, Gokul etc. Their popularity varies across
different states in India. The popularity is measured as %, with a high
popularity defined as >=90%, Medium Popularity between 50 to 90%, and
Low popularity <50%. Each brand manufactures various types of Dairy
products like milk, butter, cheese, Curd etc. The milk product can be
categorized into Low fat/medium fat or high fat content type.
1. Identify the labels and relationships, along with their properties, and
draw a high level Graph model.
2. Create nodes and relationships, along with their properties, and
visualize your actual Graph model.
3. Answer the following queries using Cypher:
a. List the names of different brands considered in your graph.
b. List the brands that are highly popular in Maharastra.
c. List the popular cheese brands in Gujarat
d. List the brands manufacturing “low” fat content milk
solution:
Creation of Graph Model
>Create(mamul:dairy{brand:"amul",pop_perc:96,state:"maharashtra"}),(mgo:da
iry{brand:"go",pop_perc:80,state:"maharashtra"}),(mbritania:dairy{brand:"brita
nia",pop_perc:55,state:"maharashtra"}),(mgokul:dairy{brand:"gokul",pop_perc:
96,state:"maharashtra"}),(gamul:dairy{brand:"amul",pop_perc:98,state:"gujrat"
}),(ggo:dairy{brand:"go",pop_perc:80,state:"gujrat"}),(gbritania:dairy{brand:"b
ritania",pop_perc:55,state:"gujrat"}),(ggokul:dairy{brand:"gokul",pop_perc:56,
state:"gujrat"})
Create milk product
>Create(p1_milk:product{pname:"milk",con_type:"LF"}),(p2_milk:product{pn
ame:"milk",con_type:"MF"}),(p3_milk:product{pname:"milk",con_type:"HF"}
)
Create cheese product
>CREATE(p1_cheese:product{pname:"cheese",con_type:"LF"}),(p2_cheese:pr
oduct{pname:"cheese",con_type:"MF"}),(p3_cheese:product{pname:"cheese",c
on_type:"HF"})
Create brands label them dairyC
>create(amul:dairy{brand:"amul",Syear:1960}),(go:dairy{brand:"go",Syear:197
0}),(britania:dairy{brand:"britania",Syear:1980}),(gokul:dairy{brand:"gokul",S
year:1990})
Create states
>CREATE (maha:state{name:"maharashtra"}),(guj:state{name:"gujrat"})
Create relation between dairy and state
>match (amul:dairy),(maha:state) where amul.brand="amul" and maha.name=
"maharashtra" create (amul)-[r1:SALES_IN{pop_perc:85}]-> (maha)
>match (amul:dairy),(guj:state) where amul.brand="amul" and guj.name=
"gujrat" create (amul)-[r1:SALES_IN{pop_perc:95}]-> (guj)
>match (go:dairy),(maha:state) where go.brand="go" and maha.name=
"maharashtra" create (go)-[r1:SALES_IN{pop_perc:45}]-> (maha)
>match (go:dairy),(guj:state) where go.brand="go" and guj.name= "gujrat"
create (go)-[r1:SALES_IN{pop_perc:75}]-> (guj)
>match (britania:dairy),(maha:state) where britania.brand="britania" and
maha.name= "maharashtra" create (britania)-[r1:SALES_IN{pop_perc:65}]->
(maha)
>match (gokul:dairy),(maha:state) where gokul.brand="gokul" and maha.name=
"maharashtra" create (gokul)-[r1:SALES_IN{pop_perc:65}]-> (maha)
Create milk Products
>CREATE(p1_milk:product:milk{name:"LFmilk",con_type:"LF"}),(p2_milk:p
roduct:milk{name:"MFmilk",con_type:"MF"}),(p3_milk:product:milk{name:"
HFmilk",con_type:"HF"})
>CREATE (p_cheese:product:cheese{name:"cheese"})
>CREATE (p_butter:product:butter{name:"butter"})
create relation between dairy and products
>match (a:dairy),(b:milk) where a.brand="amul" and b.name= "LFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="amul" and b.name= "MFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="amul" and b.name= "HFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="go" and b.name= "LFmilk" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:milk) where a.brand="britania" and b.name= "LFmilk"
create (a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:cheese) where a.brand="amul" and b.name= "cheese" create
(a)-[r1:MANUFACTURE]-> (b)
>match (a:dairy),(b:butter) where a.brand="go" and b.name= "butter" create (a)-
[r1:MANUFACTURE]-> (b)
a) List the names of different brands considered in your graph.
>match(n:dairy) return n.brand
b)List the brands that are highly popular in Maharashtra
>match (a:dairy)-[r1:SALES_IN]-> (b:state)
WHERE r1.pop_perc>=70 and b.name="maharashtra"
RETURN a.brand
c)List the popular cheese brands I Gujrath
>MATCH (a:dairy)-[r1:SALES_IN]-> (b:state) , (a:dairy)-
[r2:MANUFACTURE]-> (c:cheese)
WHERE b.name=“gujrat"
RETURN a.brand
d)List the brands manufacturing “low” and Fact content milk
>MATCH (a:dairy)-[r1:MANUFACTURE]-> (b:milk) WHERE
b.name="LFmilk"
RETURN a.brand

Q3.) Model the following as a document database A university contains


many affiliated colleges. Each college may have one or more courses like
arts, science, engineering, commerce, management, etc. There are
professors who are affiliated to one or more colleges
2. Assume appropriate attributes and collections as per the query
requirements. 3. Insert at least 10 documents in each
collection.
4. Answer the following Queries
a. Display the list of colleges conducting Arts course.
b. List names of professors who are above 40 years.
c. List the names of colleges who have more than 5 professors affiliated to
it. d. List the names of colleges
having more than 2 courses.
Solution > use uni
switched to db uni
> db.createCollection("collegeee")
{ "ok" : 1 }
>db.collegeee.insert({_id:1,"collegename":"sarhad",course:["arts","science","co
mp"],professor:[{name:"abc",age:45},{name:"pqr",age:30}]})
WriteResult({ "nInserted" : 1 })
>db.collegeee.insert({_id:2,"collegename":"abhinav",course:["arts","science","c
omp"],professor:[{name:"abc",age:45},{name:"qr",age:30}]})
WriteResult({ "nInserted" : 1 })
>db.collegeee.insert({_id:3,"collegename":"sp",course:["science","comp"],prof
essor:[{name:"bc",age:45},{name:"qr",age:30}]})
WriteResult({ "nInserted" : 1 })
>db.collegeee.insert({_id:4,"collegename":"dsp",course:["science","comp"],pro
fessor:[{name:"abc",age:45},{name:"qr",age:30}]})
WriteResult({ "nInserted" : 1 })
>db.collegeee.insert({_id:5,"collegename":"dspe",course:["arts","science","com
p"],professor:[{name:"aabc",age:45},{name:"wqr",age:50}]})
WriteResult({ "nInserted" : 1 })

2)> db.collegeee.find({professor:{$elemMatch:{age:{$gt:40}}}})
{ "_id" : 1, "collegename" : "sarhad", "course" : [ "arts", "science", "comp" ],
"professor" : [ { "name" : "abc", "age" : 45 }, { "name" : "pqr", "age" : 30 } ] }
{ "_id" : 2, "collegename" : "abhinav", "course" : [ "arts", "science", "comp" ],
"professor" : [ { "name" : "abc", "age" : 45 }, { "name" : "qr", "age" : 30 } ] }
{ "_id" : 3, "collegename" : "sp", "course" : [ "science", "comp" ], "professor" :
[ { "name" : "bc", "age" : 45 }, { "name" : "qr", "age" : 30 } ] }
{ "_id" : 4, "collegename" : "dsp", "course" : [ "science", "comp" ], "professor" :
[ { "name" : "abc", "age" : 45 }, { "name" : "qr", "age" : 30 } ] }
{ "_id" : 5, "collegename" : "dspe", "course" : [ "arts", "science", "comp" ],
"professor" : [ { "name" : "aabc", "age" : 45 }, { "name" : "wqr", "age" : 50 } ] }

1)> db.collegeee.find({course:"arts"}).pretty()
{
"_id" : 1,
"collegename" : "sarhad",
"course" : [
"arts",
"science",
"comp"
],
"professor" : [
{
"name" : "abc",
"age" : 45
},
{
"name" : "pqr",
"age" : 30
}
]
}
{
"_id" : 2,
"collegename" : "abhinav",
"course" : [
"arts",
"science",
"comp"
],
"professor" : [
{
"name" : "abc",
"age" : 45
},
{
"name" : "qr",
"age" : 30
}
]
}
{
"_id" : 5,
"collegename" : "dspe",
"course" : [
"arts",
"science",
"comp"
],
"professor" : [
{
"name" : "aabc",
"age" : 45
},
{
"name" : "wqr",
"age" : 50 }]}
Q4) Model the following Hospitals information as a graph model, and
answer the following queries using Cypher. Consider hospitals in and
around Pune. Each hospital may have one or more specializations like
Pediatric, Gynaec, Orthopaedic, etc. A person can recommend/provide
review for a hospital. A doctor can be associated with one or more
hospitals. 1. Identify the labels and relationships, along with their
properties, and draw a high level Graph model.
2. Create nodes and relationships, along with their properties, and visualize
your actual Graph model.
3. Answer the Queries
a. List the names of hospitals with pediatric specialization.
b. List the Names of doctors who are visiting “Jehangir Hospital ” on
Mondays.
c. List the most recommended Hospital for Gynaec specialization.
d. List the names of people who have given a rating of (>=3) for “Jehangir
Hospital”[5]
Solution
1)> db.Hospital.insert({Hno:1,Hname:"AAA",Specialization:["Pedi
atric","Gynaec","Orthopaedic"],People:[{Pname:"PQR",Ratin
g:4},{Pname:"SDE",Rating:5}],Doctor:[{"Dname" : "WWW", "Visit" :
"Sunday"}]}) WriteResult({ "nInserted" : 1 })

2)> db.Hospital.insert({Hno:2,Hname:"BBB",Specialization:["Gyna
ec","Orthopaedic"],People:[{Pname:"POP",Rating:2},{Pname:
"SDE",Rating:3}],Doctor:[{"Dname":"XXX",Visit:"Monday"}]})
WriteResult({ "nInserted" : 1 })

3)> db.Hospital.insert({Hno:3,Hname:"CCC",Specialization:["Gyna
ec","Orthopaedic","Pediatric"],People:[{Pname:"KLO",Rating:3},{Pname:"LP
O",Rating:3}],Doctor:[{"Dname" : "XXX","Visit": "Tuesday"}]})
WriteResult({ "nInserted" : 1 })

4) > db.Hospital.find({Specialization:"Pediatric"}) b)>


db.Hospital.find({Hname:"CCC","Doctor.Visit":"Tuesday"}) c)>
db.Hospital.find({Specialization:{$not:{$size:1}},"Doctor.Dna me":"XXX"})
d) > db.Hospital.find({"People.Rating":{ $gt: 3 },Hname:"AAA" })

Q5) Model the following sales system as a document database. Consider a


set of products, customers, orders and invoices. An invoice is generated
when an order is processed. 2. Assume appropriate
attributes and collections as per the query requirements.
3. Insert at least 10 documents in each collection.
4. Answer the following Queries. a. List all products in the inventory.
b. List the details of orders with a value >10000.
c. List all the orders which has not been processed (invoice not generated).
d. List all the orders along with their invoice for “Mr. Arun Kumar”.

Solution:
> db.product.insert({name:"robot",price:12000})
WriteResult({ "nInserted" : 1 })
> db.product.insert({name:"toycar",price:2000})
WriteResult({ "nInserted" : 1 })
> db.product.insert({name:"cricketset",price:9000})
WriteResult({ "nInserted" : 1 })
> db.product.insert({name:"studymaterial",price:19000})
WriteResult({ "nInserted" : 1 })

>db.order.insert({orderno:3736,custName:"arunkumar",product:{productName:
"toycar",price:20000},order_date:"12/2/2019",stetus:"processed",Totalbill:2039
,invoice:{invoiceNO:67564,bill:2039,date:"17/2/2019"}})
WriteResult({ "nInserted" : 1 })

>db.order.insert({orderno:3737,custName:"arunkumar",product:{productName:
"robot",price:12000},order_date:"11/3/2019",stetus:"processed",Totalbill:12800
,invoice:{invoiceNO:67574,bill:12039,date:"17/3/2019"}})
WriteResult({ "nInserted" : 1 })

>db.order.insert({orderno:3738,custName:"arunkumar",product:{productName:
"cricketset",price:9000},order _date:"15/5/2019",stetus:"in
process",Totalbill:9050})
WriteResult({ "nInserted" : 1 })

>db.order.insert({orderno:3739,custName:"mukeshpatil",product:{productNam
e:"studentmaterial",price:19000} ,order_date:"15/8/2019",stetus:"in
process",Totalbill:19080}) WriteResult({ "nInserted" : 1 })
4)
a)> db.product.find().pretty()
b) > db.order.find({Totalbill:{$lt:10000}})
c) > db.order.find({stetus:"in process"})
d) >db.order.find({custName:"arun kumar",stetus:"processed"})

Q6)Model the following Tours information as a document database. A tour


will consider the source and destination. Destination may be all around the
world. The tours are planned using different tourism industries. The
industries provide the complete information before selecting a particular
package. Customers select different packages according to their
requirements and can rate/review the tourism industry.
2. Assume appropriate attributes and collections as per the query
requirements. 3. Insert at least 10 documents in each
collection.
4. Answer the following Queries.
a. List the details of packages provided by kesari.
b. List the highest rated tourism industry.
c. List all the details of expenses made by John on his first 3 trips. Also
display the total expenses.
d. List the names of the customers who went on a tour to Shillong.
Solution:
>db.turisum.insert({name:"veenaword",rate:9,package:[{pname:"shillong",cost:
10000},{pname:"gujart",cost:7000},{pname:"karnataka",cost:6000}]})
>db.turisum.insert({name:"rohit",rate:7,package:[{pname:"shillong",cost:10000
},{pname:"rujan",cost:7000}]})
> db.tour.insert({sourc:"john",destination:"shillong",toerisumName:"veena
word",tourisumrate:8000,expense:20000,year:2018,customer:[{cname:"mukesh
",city:"pune"},{cname:"abhijeet
sangita",city:"baramati"},{cname:"manisha",city:"15no"},{cna
me:"manasi",city:"latur"}]})
>db.tour.insert({sourc:"john",destination:"karnataka",toerisumName:"veenawor
d",tourisumrate:80090,expense:20900,year:2017,customer:[{cname:"mukesh",c
ity:"pune"},{cname:"abhijeetsangita",city:"baramati"},{cname:"manisha",city:"
15no"},{cname:"manasi",city:"latur"}]})
>db.tour.insert({sourc:"john",destination:"rajasthan",toerisumName:"rohit",tour
isumrate:6000,expense:30400,year:2019,c
ustomer:[{cname:"mukesh",city:"pune"},{cname:"abhijeet
sangita",city:"baramati"},{cname:"manisha",city:"15no"},{cna
me:"manasi",city:"latur"}]})

>db.tour.insert({sourc:"john",destination:"taj",toerisumName:"rohit",tourisumra
te:60090,expense:10400,year:2016,custo
mer:[{cname:"mukesh",city:"pune"},{cname:"abhijeet
sangita",city:"baramati"},{cname:"manisha",city:"15no"},{cna
me:"manasi",city:"latur"}]})
4)
a) >db.turisum.find({name:"veena word"}).pretty()
b) >db.turisum.find({}).sort({"rate":-1}).limit(1)
c)>db.tour.aggregate([{"$sort":{"year":1}},{"$limit":3},{$group:{_id:null,"cou
nt":{"$sum":"$expense"}}}])
d) > db.tour.find({destination:"shillong"})

Q8) Model the following Online Mobile Shopping information as a


document database. Consider online mobile shopping where the customer
can get different models from different brands. Customers can rate the
brands and the models individually.
2. Assume appropriate attributes and collections as per the query
requirements.
3. Insert at least 10 documents in each collection.
4. Answer the following Queries.
a. List the mobiles having RAM and ROM as 3GB and 32GB.
b. List the customers who bought Samsung J6.
c. List the names of the distinct brands available. Also display the name of
the brand with highest rating.
d. List all the customers in ascending order who bought iPhone 7plus.
solution:
> db.custome.insert({cname:"mukesh",modelname:"samsung
j6",amount:20000}) WriteResult({ "nInserted" : 1 })
> db.custome.insert({cname:"abhijeet",modelname:"samsung
j6",amount:20060}) WriteResult({ "nInserted" : 1 })
> db.custome.insert({cname:"manasi",modelname:"iphone 7+",amount:30060})
WriteResult({ "nInserted" : 1 })
> db.custome.insert({cname:"manisha",modelname:"iphone
7+",amount:30070}) WriteResult({ "nInserted" : 1 })
> db.custome.insert({cname:"dipak",modelname:"iphone 7+",amount:30800})
WriteResult({ "nInserted" : 1 })
>db.shopping.insert({brandname:"samsung",rate:6,model:[{mname:"s40",ram:"
3GB",rom:"32GB",rate:4},{mname:"j6",ram:"4GB",rom:"32GB",rate:7},{mna
me:"j7",ram:"6GB",rom:"64 GB",rate:6}]})
WriteResult({ "nInserted" : 1 })
>db.shopping.insert({brandname:"vivo",rate:8,model:[{mname:"Y55",ram:"3G
B",rom:"32GB",rate:6},{mname:"Ys5",ram:"4
GB",rom:"32GB",rate:4},{mname:"YYY",ram:"6GB",rom:"64G B",rate:6}]})
WriteResult({ "nInserted" : 1 })
4)
a) > db.shopping.find({"model.ram":"3GB","model.rom":"32GB"})
b) > db.custome.find({modelname:"samsung j6"})
c) >
db.shopping.aggregate([{"$sort":{"rate":1}},{"$limit":1},{$group:{_id:"$brand
name"}}]) d) > db.custome.find().sort( { "cname": 1 } )

You might also like