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