Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Daniela Pistol

Queries
Task 1

a) The person named Anabela Teixeira


▪ Query: db.movies.find({"person-name" : "Teixeira, Anabela" }).pretty()

b) The birthplace of Steven Spielberg


▪ Query: db.movies.find({"person-name" : "Spielberg, Steven" }, {
"info.birthnotes" : 1}).pretty()

c) The number of people born in Lisbon


▪ Query: db.movies.find({"info.birthnotes" : { $regex : ".*Lisbon.*" } }).count()

d) The people taller than 170 cm.


▪ Query1:
db.movies.find().forEach( function (el) {
let height;
//save the value for info.height
let str = el["info"]["height"]+'';
//if the height it's not defined we don't modify the element
if(str !='undefined'){
//if it's defined, then we verify if it's in ft, inch or in cm
//we split by the ' to verify if it's in ft
//and if it's in both ft and inch than we will have in splitted[0] the value
//of ' and in splitted[1] the rest of the string
if(str.search("'") == 1){
splitted = str.split("'");
//if it's in incgh, we verify if we have a division at inch, like 1/2
if(splitted[1].search("/") != -1){
//we convert the value from ' in cm
height = 30.48*parseInt(splitted[0]);
//making a split after what remains after ' to separate the int
//from the division part for instance,
//if we have "4' 12 1/2" we split in 4 and 12 1/2
//delete the empty elements if exists
inch = splitted[1].split(" ").filter(n => n);
let id = inch[1] + '';
//transforming from division in int
i = parseInt(inch[0])+eval(inch[1]);
//convert in cm the value of '
Daniela Pistol

height = height + i * 2.54;


//keep to numbers after the ,
height = height.toFixed(2);
} else {
height = 30.48*parseInt(splitted[0]);
inch = splitted[1].replace(/\s/g, '');
if(str.length > 2){
height = height + parseInt(inch)*2.54;
}
height = height.toFixed(2);
}
} else{
height = parseInt(str);
}
el["info"]["height"] = height + " cm";
db.movies.save(el);
}
});

▪ Query2: db.movies.find(function() { return this.info.height > "170 cm"},


{"person-name":1, _id:0, "info.height":1}).pretty()

e) The names of people whose information contains “Opera”


▪ Query:
db.movies.find().forEach(
function(doc) {

for(elem in doc){
if (elem == "info"){
for(child in doc[elem]){
let s = doc[elem][child]+'';
if(s.includes("Opera") ){
print(doc["person-name"]);
break; //if a field contains 2 “opera” to print it only once.
}
}
}
}
}
);
Daniela Pistol

f) For each movie person whose birth place is known, the latitude, longitude
and population of that city (if that information exists in the city document)
▪ Query:
db.movies.aggregate([
{
$unwind: "$info.birthnotes"
},
{ $project : { birthplace : { $split: ["$info.birthnotes", ", "] }, _id:0 , "person-
name":1}},

{ $project:
{ city : { $arrayElemAt : [ "$birthplace", 0 ]}, "person-name":1 }
},
{
$lookup:{
from: "cities",
localField: "city",
foreignField: "name",
as: "infocity"
}
},
{
$project:{
"person-name":1,
"city":1,
"infocity.population":1,
"infocity.location":1
}
}
]);

You might also like