NoSqlZoo答案(未完待续)

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.

  1. Use find() to show the details of Germany.
    Show Germany instead of France.
db.world.find({
  name: 'Germany'
});
  1. 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();
  1. You can test numbers as well as strings.
    Find the country with an area of exactly 43094.
db.world.find({
  area: 43094
}).pretty();
  1. 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();
  1. 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();
  1. 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
});
  1. 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.

  1. Give the name and the per capita GDP for those countries with a population 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']}
    }}
]);
  1. Give the name and the population 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"]}
    }}
]);
  1. Give the name and the population 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"]
    }
  }
}]);
  1. Show the name and population 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]}
  }
}]);
  1. Show the name and population 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"]
    }
  }
}]);
  1. Order the continents by area from most to least.
db.world.aggregate([
    {$group: {
        _id: "$continent",
        area: {$sum: "$area"}
    }},
    {$sort: {
        area: -1
    }},
    {$project: {
        _id: 1,
        area: 1
    }}
]);
  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
}]);
  1. 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
  }
}]);
  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” ] }


3. Mapreduce Tutorial

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值