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 videodb.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字段:

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 100YWeatherSmalldb.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 shipsdb.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 100YWeatherSmalldb.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 100YWeatherSmalldb.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 resultsdb.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}]
}

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

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

  1. M001: MongoDB Basics chapter 2 The MongoDB Query Language + Atlas学习记录

    M001: MongoDB Basics chapter 2 The MongoDB Query Language + Atlas学习记录 运行环境 操作系统:windows 10 家庭中文版 Mon ...

  2. M102: MongoDB for DBAs chapter 2 crud_and_administrative_commands学习记录

    M102: MongoDB for DBAs chapter 2 crud_and_administrative_commands 运行环境 操作系统:windows 10 家庭中文版 Mongodb ...

  3. M102: MongoDB for DBAs chapter 3 performance学习记录

    M102: MongoDB for DBAs chapter 3 performance学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mon ...

  4. M102: MongoDB for DBAs chapter 1 introduction学习记录

    M102: MongoDB for DBAs chapter 1 introduction 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongod ...

  5. M201: MongoDB Performance chapter 4 CRUD Operations学习记录

    M201: MongoDB Performance chapter 4 CRUD Operations学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb ...

  6. M102: MongoDB for DBAs chapter 4 replication学习记录

    M102: MongoDB for DBAs chapter 4 replication 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 Mongodb ...

  7. M201: MongoDB Performance chapter 1 Introduction学习记录

    M201: MongoDB Performance chapter 1 Introduction学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb 3.4 ...

  8. M201: MongoDB Performance chapter 3 Index Operations学习记录

    M201: MongoDB Performance chapter 3 Index Operations学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb ...

  9. M201: MongoDB Performance chapter 2 Mongodb Indexes学习记录

    M201: MongoDB Performance chapter 2 Mongodb Indexes学习记录 运行环境 操作系统:windows 10 家庭中文版 Mongodb :Mongodb ...

最新文章

  1. 用cxSelect插件补充一下回显过滤项功能
  2. qq远程控制电脑方法_【技术分享】教你远程控制别人电脑
  3. 您从未听说过的Java 8的10个功能
  4. makefile的两个变量(自动变量和普通变量)
  5. 如何在Chrome浏览器中创建账户?
  6. Airflow 中文文档:使用操作器
  7. (四)Ubuntu 14.04 文件服务器--samba的安装和配置
  8. 操作系统原理学习总结
  9. 【181023】VC++开发的电路板画图设计软件源代码
  10. matlab 打开文件bin
  11. 中国广电即将放号,感受到压力的中国移动率先推出19元5G套餐
  12. 由三点画圆到未来日记:失控中的位置隐私
  13. 算法题——立方体的体对角线穿过多少个正方体?
  14. 双连通分量的题目列表(一)
  15. 简单的Winform秒表工具
  16. 正弦分析2--三角函数线
  17. mysql建库1044_Mysql创建数据库时提示Error 1044
  18. 七牛云配置token-----CryptoJS.js
  19. 调查上网行为管理软件或设备
  20. 家电三巨头的集体突围

热门文章

  1. 广域网技术-PPP协议
  2. 【装箱问题】基于Shuffled Complex Evolution (SCE) 算法解决装箱问题 (BPP)附matlab代码
  3. 使用js在线将pdf转为图片
  4. 【软工作业思考】关于软工的一些概念性理解暨第一次阅读作业
  5. mysql move table_关于move table和rebuild index批量操作的记录
  6. html网页随机一言,搭建Hitokoto网站·一言经典语句功能及调用案例
  7. 解决 macbook m1 苹果笔记本电脑 关闭盖子 休眠
  8. Origin画图技巧之放大局域图技巧2
  9. minio 上传文件失败报错信息: The difference between the request time and the server‘s time is too large.
  10. 麒麟信安天机存储加密系统——国家密码管理局商用密码认证产品