1. Find Tutorial
This tutorial introduces NoSQL using MongoDB. We will be using the find()
command and comparison functions on the collection world which contains details of around 250 countries of the world:
Find more examples are available.
- Use
find()
to show the details of Germany.
Show Germany instead of France.
db.world.find({
name: 'Germany'
});
- You can use
pretty()
to make the output more readable.
List all the countries in the continent of “Eurasia”.
db.world.find({
continent: 'Eurasia'
}).pretty();
- You can test numbers as well as strings.
Find the country with an area of exactly 43094.
db.world.find({
area: 43094
}).pretty();
- You can use
$gt
(greater than) and$lt
(less than) to compare numbers and strings.
Show each country with a population of over 250000000.
Sort the results alphabetically.
You will need to use a projection to answer this question.
db.world.find(
{population: {$gt: 250000000}},
{name: 1, _id: 0}
).sort(
{name: 1}
).pretty();
- Greater than and less than comparisons can also be applied to strings.
List the countries that come after “S” in the alphabet.
Learn more about RegEx: https://nosqlzoo.net/wiki/RegEx_Pattern_Matching
db.world.find(
{name: {'$regex': "^S"}},
{name: 1,
_id: 0}
).pretty();
- Find the name and capital cities for countries with a population of over 70 million.
db.world.find({
population: {
$gt: 70000000
}
}, {
name: 1,
capital: 1,
_id: 0
});
- Find the countries that have a population that is over 200 million or less than 20,000.
db.world.find({
$or: [{
population: {
$lt: 20000
}
}, {
population: {
$gt: 200000000
}
}]
}, {
name: 1,
population: 1,
_id: 0
})
2. Aggregate Tutorial
For these questions you should use aggregate([])
on the collection world
You may find these Aggregate examples useful.
- Give the
name
and theper capita
GDP for those countries with apopulation
of at least 200 million.
How to calculate per capita GDP.
db.world.aggregate([
{$match: {
population: {$gte: 200000000}
}},
{$project: {
_id: 0,
name: 1,
"per capita GDP": {$divide: ['$gdp', '$population']}
}}
]);
- Give the
name
and thepopulation density
of all countries in South America.
How to calculate population density
db.world.aggregate([
{$match: {continent: 'South America'}},
{$project: {
_id: 0,
name: 1,
density: {$divide: ["$population", "$area"]}
}}
]);
- Give the
name
and thepopulation density
of all countries with name after V in the alphabet.
Note that because Vatican City (with area 0) is in Europe you will get a divide by zero error unless you filter first.
db.world.aggregate([{
$match: {
name: {
$gt: 'V'
},
area: {
"$ne": 0
}
}
}, {
$project: {
_id: 0,
name: 1,
density: {
$divide: ["$population", "$area"]
}
}
}]);
- Show the
name
andpopulation
in millions for the countries of the continent South America.
Divide the population by 1000000 to get population in millions.
db.world.aggregate([{
$match: {
continent: "South America"
}
}, {
$project: {
_id: 0,
name: 1,
population: {$divide: ["$population", 1000000]}
}
}]);
- Show the
name
andpopulation
density for France, Germany, and Italy
db.world.aggregate([{
$match: {
name: {
$in: ['France', 'Germany', 'Italy']
},
population: {
$ne: null
},
area: {
$ne: 0
}
}
}, {
$project: {
_id: 0,
name: 1,
"population density": {
$divide: ["$population", "$area"]
}
}
}]);
- Order the
continents
byarea
from most to least.
db.world.aggregate([
{$group: {
_id: "$continent",
area: {$sum: "$area"}
}},
{$sort: {
area: -1
}},
{$project: {
_id: 1,
area: 1
}}
]);
- Show the only two continents with total area greater than 25000000 and then sort from largest to smallest.
db.world.aggregate([{
$match: {}
}, {
$group: {
_id: "$continent",
area: {
$sum: "$area"
},
}
}, {
$sort: {
area: -1
}
}, {
$project: {
_id: 1,
name: 1,
area: 1
}
}, {
$limit: 2
}]);
- For each continent show the first and last country alphabetically like this:
{ “_id” : “Africa”, “from” : “Algeria”, “to” : “Zimbabwe” }
{ “_id” : “Asia”, “from” : “Afghanistan”, “to” : “Yemen” }
{ “_id” : “Caribbean”, “from” : “Antigua and Barbuda”, “to” : “Trinidad and Tobago” }
{ “_id” : “Eurasia”, “from” : “Armenia”, “to” : “Russia” }
{ “_id” : “Europe”, “from” : “Albania”, “to” : “Vatican City” }
{ “_id” : “North America”, “from” : “Belize”, “to” : “United States” }
{ “_id” : “Oceania”, “from” : “Australia”, “to” : “Vanuatu” }
{ “_id” : “South America”, “from” : “Argentina”, “to” : “Venezuela” }
db.world.aggregate([{
$group: {
_id: "$continent",
from: {$min: "$name"},
to: {$max: "$name"}
}
}, {
$sort: {
_id: 1,
from: 1,
to: 1
}
}]);
- Group countries according to the first letter of the name. As shown. Only give “U” through to “Z”.
You will need to use the$substr
function and the$push
aggregate function.
{ “_id” : “U”, “list” : [ “Uganda”, “Ukraine”, “United Arab Emirates”, “United Kingdom”, “United States”, “Uruguay”, “Uzbekistan” ] }
{ “_id” : “V”, “list” : [ “Vanuatu”, “Vatican City”, “Venezuela”, “Vietnam” ] }
{ “_id” : “Y”, “list” : [ “Yemen” ] }
{ “_id” : “Z”, “list” : [ “Zambia”, “Zimbabwe” ] }