M001: MongoDB Basics Chapter 3: Deeper Dive on the MongoDB Query Language学习记录

M001: MongoDB Basics Chapter 3: Deeper Dive on the MongoDB Query Language学习记录

运行环境

操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4

Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data

课后问题

Comparison Operators(比较运算符)

Problem:
Using the $in operator, filter the video.movieDetails collection to determine how many movies list either “Ethan Coen” or “Joel Coen” among their writers. Your filter should match all movies that list either of the Coen brothers as writers regardless of how many other writers are also listed. Select the number of movies matching this filter from the choices below.

Check all answers that apply:

Attempts Remaining:Correct Answer

  • 0
  • 3
  • 7
  • 12
  • 16

解答

In the mongo shell, assuming you’ve loaded movieDetails into the video database and assuming you are connected to your Atlas sandbox cluster, you can issue the following commands.

use video

db.movieDetails.find({writers: {$in: ["Ethan Coen", "Joel Coen"]}}).count()

In Compass, navigate to the video.movieDetails collection in your Atlas sandbox cluster. Then apply the following filter in either the Schema view or Documents view.

{writers: {$in: ["Ethan Coen", "Joel Coen"]}}

答案为3

相关示例

runtime大于90:

db.movieDetails.find({runtime: {$gt: 90}})

runtime大于90,指定返回title,runtime字段:

db.movieDetails.find({runtime: {$gt: 90}}, {_id: 0, title: 1, runtime: 1})

runtime大于90小于120,指定返回title,runtime字段:

扫描二维码关注公众号,回复: 624105 查看本文章
db.movieDetails.find({runtime: {$gt: 90, $lt: 120}}, {_id: 0, title: 1, runtime: 1})

runtime大于等于90小于等于120,指定返回title,runtime字段:

db.movieDetails.find({runtime: {$gte: 90, $lte: 120}}, {_id: 0, title: 1, runtime: 1})

runtime大于等于180且tomato.meter为100,指定返回title,runtime字段:

db.movieDetails.find({runtime: {$gte: 180}, "tomato.meter": 100}, {_id: 0, title: 1, runtime: 1})

rated不等于UNRATED,指定返回title,rated字段:

db.movieDetails.find({rated: {$ne: "UNRATED"}}, {_id: 0, title: 1, rated: 1})

rated为G或者PG,指定返回title,rated字段:

db.movieDetails.find({rated: {$in: ["G", "PG"]}}, {_id: 0, title: 1, rated: 1})

rated为G或者PG或者PG-13,指定返回title,rated字段,用pretty格式:

db.movieDetails.find({rated: {$in: ["G", "PG", "PG-13"]}}, {_id: 0, title: 1, rated: 1}).pretty()

rated为R或者PG-13,指定返回title,rated字段,用pretty格式:

db.movieDetails.find({rated: {$in: ["R", "PG-13"]}}, {_id: 0, title: 1, rated: 1}).pretty()

Element Operators(元素操作符)

Problem:
Connect to our class Atlas cluster from the mongo shell or Compass and answer the following question. How many documents in the 100YWeatherSmall.data collection do NOT contain the key atmosphericPressureChange.

Choose the best answer:

Attempts Remaining:Correct Answer

  • 1
  • 2679
  • 10345
  • 33989
  • 40668

解答

In the mongo shell, assuming you’ve connected to the M001 class Atlas cluster, you can issue the following commands to find this value.

use 100YWeatherSmall

db.data.find({atmosphericPressureChange: {$exists: false}}).count()

In Compass, navigate to the 100YWeatherSmall.data collection and then apply the following filter in either the Schema or Documents view.

{atmosphericPressureChange: {$exists: false}}

答案为40668

示例

存在字段mpaaRating:

db.moviesDetails.find({mpaaRating: {$exists: true}})

不存在字段mpaaRating:

db.moviesDetails.find({mpaaRating: {$exists: false}})

mpaaRating为空的:

db.movieDetails.find({mpaaRating: null})

查询所有:

db.movieDetails.find({})

viewerRating字段类型为int:

db.movies.find({viewerRating: {$type: "int"}}).pretty()

viewerRating字段类型为double:

db.movies.find({viewerRating: {$type: "double"}}).pretty()

Logical Operators(逻辑运算符)

Problem:
Connect to our class Atlas cluster from the mongo shell or Compass and view the ships.shipwrecks collection. In this collection, watlev describes the water level at the shipwreck site and depth describes how far below sea level the ship rests. How many documents in the ships.shipwrecks collection match either of the following criteria: watlev equal to “always dry” or depth equal to 0.

Choose the best answer:

Attempts Remaining:Correct Answer

  • 501
  • 1644
  • 2000
  • 2331
  • 3105

解答

In the mongo shell, assuming you’ve connected to the M001 class Atlas cluster, you can issue the following commands to find this value.

use ships

db.shipwrecks.find({$or: [{depth: 0}, {watlev: "always dry"}]}).count()

In Compass, navigate to the ships.shipwrecks collection and then apply the following filter in either the Schema or Documents view.

{$or: [{depth: 0}, {watlev: "always dry"}]}

答案为2331

示例

tomato.meter大于95或者metacritic大于88,显示title,tomato.meter,metacritic:

db.movieDetails.find({$or: [{"tomato.meter": {$gt: 95}},                               
                            {"metacritic": {$gt: 88}}]},
                     {_id: 0, title: 1, "tomato.meter": 1, "metacritic": 1})

tomato.meter大于95且metacritic大于88,显示title,tomato.meter,metacritic:

db.movieDetails.find({$and: [{"tomato.meter": {$gt: 95}},                               
                            {"metacritic": {$gt: 88}}]},
                     {_id: 0, title: 1, "tomato.meter": 1, "metacritic": 1})

tomato.meter大于95且metacritic大于88,显示title,tomato.meter,metacritic:

db.movieDetails.find({"tomato.meter": {$gt: 95},                               
                      "metacritic": {$gt: 88}},
                     {_id: 0, title: 1, "tomato.meter": 1, "metacritic": 1})

metacritic不为空且存在metacritic字段,显示title,metacritic:

db.movieDetails.find({$and: [{"metacritic": {$ne: null}},
                             {"metacritic": {$exists: true}}]},
                          {_id: 0, title: 1, "metacritic": 1})

metacritic为空且存在metacritic字段,显示title,metacritic:

db.movieDetails.find({$and: [{"metacritic": null},
                             {"metacritic": {$exists: true}}]},
                     {_id: 0, title: 1, "metacritic": 1})

Array Operators(数组运算符): $all

all最大的好处是不要求元素的顺序一致

Problem:
Connect to our class Atlas cluster from the mongo shell or Compass and view the 100YWeatherSmall.data collection. The sections field in this collection identifies supplementary readings available in a given document by a three-character code. How many documents list: “AG1”, “MD1”, and “OA1” among the codes in their sections array. Your count should include all documents that include these three codes regardless of what other codes are also listed.

Choose the best answer:

Attempts Remaining:Correct Answer

  • 2000
  • 9803
  • 10200
  • 15442
  • 17348

解答

In the mongo shell, assuming you’ve connected to the M001 class Atlas cluster, you can issue the following commands to find this value.

use 100YWeatherSmall

db.data.find({sections: {$all: ["AG1", "MD1", "OA1"]}}).count()

In Compass, navigate to the 100YWeatherSmall.data collection and then apply the following filter in either the Schema or Documents view.

{sections: {$all: ["AG1", "MD1", "OA1"]}}

答案为10200

示例

genres中含有”Comedy”, “Crime”, “Drama”,显示title,genres,以pretty格式:

db.movieDetails.find({genres: {$all: ["Comedy", "Crime", "Drama"]}}, 
                     {_id: 0, title: 1, genres: 1}).pretty()

Array Operators(数组运算符): $size

Problem:
Connect to our class Atlas cluster from the mongo shell or Compass and view the 100YWeatherSmall.data collection. How many documents in this collection contain exactly two elements in the sections array field?

Choose the best answer:

Attempts Remaining:Correct Answer

  • 114
  • 670
  • 2656
  • 10700
  • 25678

解答

In the mongo shell, assuming you’ve connected to the M001 class Atlas cluster, you can issue the following commands to find this value.

use 100YWeatherSmall

db.data.find({sections: {$size: 2}}).count()

In Compass, navigate to the 100YWeatherSmall.data collection and then apply the following filter in either the Schema or Documents view.

{sections: {$size: 2}}

答案为2656

示例

countries有1个元素,以pretty格式:

db.movieDetails.find({countries: {$size: 1}}).pretty()

Array Operators: $elemMatch

使多个条件语句与一个数组元素进行比较,但不会匹配非数组元素

Problem:

In the M001 class Atlas cluster you will find a database added just for this week of the course. It is called results. Within this database you will find two collections: surveys and scores. Documents in the results.surveys collection have the following schema.

{_id: ObjectId(“5964e8e5f0df64e7bc2d7373”),
results: [{product: “abc”, score: 10}, {product: “xyz”, score: 9}]}

The field called results that has an array as its value. This array contains survey results for products and lists the product name and the survey score for each product.

How many documents in the results.surveys collection contain a score of 7 for the product, “abc”?

Choose the best answer:

Attempts Remaining:Correct Answer

  • 35
  • 124
  • 172
  • 220
  • 301

解答

In the mongo shell, assuming you’ve connected to the M001 class Atlas cluster, you can issue the following commands to find this value.

use results

db.surveys.find({results: {$elemMatch: {product: "abc", score: 7}}}).count()

Note that it is incorrect to use the following query.

db.surveys.find({"results.product": "abc", "results.score": 7})

because in addition to correct results, this will return the document.

{"_id": 4, "results": [{"product": "abc", "score": 8}, {"product": "xyz", "score": 7}]}

This document does contain an entry for “abc” and a score of 7 in the results array, but the 7 is the score of the “xyz” product, not “abc”.

In Compass, navigate to the results.surveys collection and then apply the following filter in either the Schema or Documents view.

{results: {$elemMatch: {product: "abc", score: 7}}}

答案为124

示例

假设有如下数据:

> use my_test
switched to db my_test

> db.m00103.find().pretty()
{
        "_id" : ObjectId("5ad6c660e9de5b56a3e0f564"),
        "boxOffice" : [
                {
                        "country" : "USA",
                        "revenue" : 228.4
                },
                {
                        "country" : "Australia",
                        "revenue" : 19.6
                },
                {
                        "country" : "UK",
                        "revenue" : 33.9
                },
                {
                        "country" : "Germany",
                        "revenue" : 16.2
                },
                {
                        "country" : "France",
                        "revenue" : 19.8
                }
        ]
}

执行查询:

> db.m00103.find({"boxOffice.country": "Germany", "boxOffice.revenue": {$gt: 17}}).pretty()
{
        "_id" : ObjectId("5ad6c660e9de5b56a3e0f564"),
        "boxOffice" : [
                {
                        "country" : "USA",
                        "revenue" : 228.4
                },
                {
                        "country" : "Australia",
                        "revenue" : 19.6
                },
                {
                        "country" : "UK",
                        "revenue" : 33.9
                },
                {
                        "country" : "Germany",
                        "revenue" : 16.2
                },
                {
                        "country" : "France",
                        "revenue" : 19.8
                }
        ]
}

执行查询:

> db.m00103.find({"boxOffice.country": "Germany", "boxOffice.revenue": {$gt: 228}}).pretty()
{
        "_id" : ObjectId("5ad6c660e9de5b56a3e0f564"),
        "boxOffice" : [
                {
                        "country" : "USA",
                        "revenue" : 228.4
                },
                {
                        "country" : "Australia",
                        "revenue" : 19.6
                },
                {
                        "country" : "UK",
                        "revenue" : 33.9
                },
                {
                        "country" : "Germany",
                        "revenue" : 16.2
                },
                {
                        "country" : "France",
                        "revenue" : 19.8
                }
        ]
}

从上面的查询可以看出,只要boxOffice数组中有满足任何一个条件的元素就会显示整个数组,查询条件会匹配多个数组元素

下面试试$elemMatch参数:

> db.m00103.find({boxOffice: {$elemMatch: {"country": "Germany", "revenue": {$gt: 17}}}}).pretty()
> db.m00103.find({boxOffice: {$elemMatch: {"country": "Germany", "revenue": {$gt: 16}}}}).pretty()
{
        "_id" : ObjectId("5ad6c660e9de5b56a3e0f564"),
        "boxOffice" : [
                {
                        "country" : "USA",
                        "revenue" : 228.4
                },
                {
                        "country" : "Australia",
                        "revenue" : 19.6
                },
                {
                        "country" : "UK",
                        "revenue" : 33.9
                },
                {
                        "country" : "Germany",
                        "revenue" : 16.2
                },
                {
                        "country" : "France",
                        "revenue" : 19.8
                }
        ]
}

只有当某一数组元素满足所有查询条件才会输出数组

猜你喜欢

转载自blog.csdn.net/sunbocong/article/details/79987357
今日推荐