mongoDB练习题

曾经课上的ADB查询练习,做个备份。
(然后发现自己依然有两个不会写[捂脸])
//
// Using the video.movieDetails collection, write the MongoDB queries that return the information below:
//

Query 01
// the number of document in the collection

db.movieDetails.find().count()

Query 02
// the first 20 documents in the collection (plain and pretty printing)

db.movieDetails.find().limit(20).pretty()

Query 03
// the first 10 documents in the collection

db.movieDetails.find().limit(10).pretty()

Query 04
// the id and the title of the first 5 movies

db.movieDetails.find({type:"movie"},{title:true,"imdb.id":true}).limit(5).pretty()

Query 05
// the title and the director of the first 5 movies

db.movieDetails.find({type:"movie"},{title:true,director:true}).limit(5).pretty()

Query 06
// the movies whose runtime is exactly 1 hour (60 minutes) - 2 methods

db.movieDetails.find({type:"movie",runtime:60},{title:true,runtime:true}).pretty()
db.movieDetails.find({type:"movie",runtime:{$eq:60}},{title:true,runtime:true}).pretty()

Query 07
// the movies whose runtime is greater than or equal to 1 hour

db.movieDetails.find({type:"movie",runtime:{$gte:60}},{title:true,runtime:true}).pretty()

Query 08
// the movies whose runtime is between 1:00 and 1:30 (included)

db.movieDetails.find({type:"movie",runtime:{$gte:60,$lte:90}},{title:true,runtime:true}).pretty()

Query 09
// the movies directed by Sergio Leone

db.movieDetails.find({type:"movie",director:"Sergio Leone"},{title:true,director:true}).pretty()

Query 10
// the movies directed by Sergio Leone with a runtime of 229 minutes - 2 methods

db.movieDetails.find({type:"movie",director:"Sergio Leone",runtime:229},{title:true,runtime:true,director:true}).pretty()
db.movieDetails.find({type:"movie",director:"Sergio Leone",runtime:{$eq:229}}},{title:true,runtime:true,director:true}).pretty()

Query 11
// the movies whose title contains a ‘t’

db.movieDetails.find({type:"movie",title:/c/},{title:true}).pretty()

Query 12
// the movies whose title contains a ‘t’ in the 4th position

db.movieDetails.find({type:"movie",title:/4t/},{title:true}).pretty()

Query 13
// the movies whose title begins with ‘T’ and ends with with ‘c’

db.movieDetails.find({type:"movie",title:/^T/,title:/c$/},{title:true}).pretty()

Query 14
// the movies whose title begins with ‘T’ and ends with with ‘c’ and contains a ‘B’ followed by a ‘i’

db.movieDetails.find({type:"movie",title:/^T/,title:/c$/,title:/Bi/},{title:true}).pretty()

Query 15
// the movies whose duration is less than 1 hour or were released before 1968

db.movieDetails.find({type:"movie",$or:[runtime:{$lte:60},year:{$lte:1968}]},{title:true,runtime:true,year:true}).pretty()

Query 16
// the movies with exactly 3 actors

db.movieDetails.find({type:"movie",actors:{$size:3}},{title:true}).pretty()

Query 17
// the movies featuring Harrison Ford

db.movieDetails.find({actors:"Harrison Ford"},{title:true,actors:true}).pretty()

Query 18
// the movies staring Harrison Ford (i.e. he’s the first in list)

db.movieDetails.find({"actors.0":"Harrison Ford"},{title:true,actors:true}).pretty()

Query 19
// the movies with Harrison Ford as the second actor

db.movieDetails.find({"actors.1":"Harrison Ford"},{title:true,actors:true}).pretty()

Query 20
// the movies featuring both Harrison Ford and Paul Freeman

db.movieDetails.find({actors:{$eq:"Harrison Ford",$eq:"Paul Freeman"}},{title:true,actors:true}).pretty()

Query 21
// the movies featuring Harrison Ford but not Carrie Fisher

db.movieDetails.find({actors:"Harrison Ford",actors:{$ne:"Carrie Fisher"}},{title:true,actors:true}).pretty()

Query 22
// the movies featuring actors whose name a ‘a’ and a ‘r’

Query 23
// same question as above, this time using 2 single-letter regular expression

Query 24
// the various ratings (field ‘rated’) in the collection

db.movieDetails.distinct("rated")

Query 25
// the movies with no awards specification

db.movieDetails.find({awards:null,{title:true,awards:true}).pretty()

Query 26
// the movies with over 10 award nominations

db.movieDetails.find({"awards.nominations":{$gt:10},{title:true,"awards.nominations":true}).pretty()

Query 27
// the greatest movie runtime (output only the runtime, not the movie description)

db.movieDetails.find({},{runtime:true,_id:false}).sort({"runtime":-1}).limit(1)

Query 28
// the smallest, average and greatest movie runtime (output only the runtime, not the movie description)

db.movieDetails.aggregate([{$group:{_id:"$type",avg:{$avg:"$runtime"},max:{$max:"$runtime"},min:{$min:"$runtime"}}}]);

Query 29
// the title of the movie with the greatest runtime, and its runtime

db.movieDetails.find({},{title:true,runtime:true,_id:false}).sort({"runtime":-1}).limit(1)

Query 30
// for each director, his/her name and the number of movies he/she directed

db.movieDetails.aggregate([{$group:{_id:"$director",sum:{$sum:1}}}]);

Query 31
// the number of movies by Sergio Leone

db.movieDetails.find({director:"Sergio Leone"}).count()

Query 32
// the number of movies whose rating (field ‘rated’) is not specified, i.e. is either missing or null

db.movieDetails.find({rated:{$exists:true,$ne:null}},{title:true,rated:true})

Query 33
// the movies sorted by rating (alphabetical order) and then runtime (longest movie first)

db.movieDetails.find({rated:{$exists:true,$ne:null}},{title:true,rated:true,runtime:true}).sort(rated:1,runtime:-1)

Query 34
// the shortest movie runtime - 2 methods

db.movieDetails.find({},{runtime:true,_id:false}).sort({"runtime":1}).limit(1)
db.movieDetails.aggregate([{$sort:{runtime:-1}},{ $limit : 1 }]);

Query 35
// the smallest, average and greatest movie runtime

db.movieDetails.aggregate([{$group:{_id:"$type",avg:{$avg:"$runtime"},max:{$max:"$runtime"},min:{$min:"$runtime"}}}]);

Query 36
// the smallest, average and greatest runtime of the movies directed by Steven Spielberg

db.movieDetails.aggregate([{$match:{director:"Steven Spielberg"}},{$group:{_id:"$type",avg:{$avg:"$runtime"},max:{$max:"$runtime"},min:{$min:"$runtime"}}}]);

Query 37
// for each director, his/her name and the number of movies he/she directed, along with the total and average movie runtime for that director

db.movieDetails.aggregate([{$group:{_id:"$director",number:{$sum:1},sum:{$sum:"$runtime"},avg:{$avg:"$runtime"}}}]);

Query 38
// same question as above, with the results sorted by the number of movies (descending), then the average runtime (ascending)

db.movieDetails.aggregate([{$group:{_id:"$director",number:{$sum:1},sum:{$sum:"$runtime"},avg:{$avg:"$runtime"}}},{$sort:{number:-1,avg:1}}])

Query 39
// the non-null ratings assigned to 20 movies or more - 2 methods (which one is best?)

db.movieDetails.aggregate([{$match:{rated:{$exists:true,$ne:null} ,runtime:{$exists:true,$ne:null}}},{$group:{_id:"rated",sum:{$sum:1}}},{$match:{sum:{$gte:20}}}])

Query 40
// the largest per-rating average runtime (nulls omitted)

db.movieDetails.aggregate([ {$match: {rated:{$exists:true,$ne:null},runtime:{$exists:true,$ne:null}}},{$group:{_id:"rated",runtime_avg_max:{$avg:"$runtime"}}},{$sort:{number:-1}},{$limit:1}])

Query 41
// the ratings assigned to the largest number of movies (nulls omitted)

db.movieDetails.aggregate([{$match:{rated:{$exists:true,$ne:null}}},{$group:{_id:"$rated",number:{$sum:1}}},{$sort:{number:-1}},{$limit:1}])
### 关于MongoDB文档数据库的大数据分析第五章相关内容 在大数据分析领域,MongoDB作为一种流行的NoSQL数据库系统,其核心特性在于使用文档模型来存储和管理数据[^1]。这种设计使得它非常适合处理结构化、半结构化以及非结构化的复杂数据集。 #### MongoDB中的文档模型及其应用场景 MongoDB采用BSON(Binary JSON)作为主要的数据存储格式,支持灵活的模式定义,允许单个集合内的不同文档具有不同的字段组成。这一特点使其成为大数据场景下理想的解决方案之一,尤其是在需要快速迭代开发的应用程序中表现尤为突出。 尽管未直接提及具体章节内容或习题细节[^2],但基于常规教学安排推测,“大数据分析”课程可能围绕以下几个方面展开讨论: - **查询优化**:如何通过索引提高读写性能。 - **分片机制**:实现水平扩展以应对海量数据增长需求。 - **聚合框架**:执行复杂的多阶段操作完成统计计算任务。 以下是针对上述主题设计的一些典型练习题目示例: ```javascript // 示例代码片段展示基本查询功能 db.collection.find({ key: { $gt: value } }); // 聚合管道实例用于求平均值 db.sales.aggregate([ { $match : { category : "electronics" }}, { $group : { _id : null, avgPrice : { $avg : "$price" }}} ]); ``` 以上脚本展示了基础检索方法及高级汇总技巧的实际运用情况。 ### 提供的学习资源建议 对于希望深入理解并实践这些概念的学生来说,《NoSQL数据库技术与应用》教材及相关补充材料将是宝贵的知识来源。同时鼓励参与官方提供的在线教程项目,进一步巩固理论联系实际的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值