Skip to Content
MongoDB权威指南(第3版)
book

MongoDB权威指南(第3版)

by Shannon Bradshaw, Eoin Brazil, Kristina Chodorow
November 2021
Intermediate to advanced
410 pages
12h 1m
Chinese
Posts & Telecom Press
Content preview from MongoDB权威指南(第3版)
56
4
行进一步微调。
MongoDB 3.6
增加了
$expr
运算符,它允许在
MongoDB
查询语句中使用
聚合表达式。因为它不需要执行
JavaScript
所以速度比
$where
快,建议尽可能使用此运
算符作为替代。
进行复杂查询的另一种方法是使用聚合工具,第
7
章会详细介绍。
4.5
 游标
数据库会使用
游标
返回
find
的执行结果。游标的客户端实现通常能够在很大程度上对查询
的最终输出进行控制。你可以限制结果的数量,跳过一些结果,按任意方向的任意键组合
对结果进行排序,以及执行许多其他功能强大的操作。
要使用
shell
创建游标,
首先要将一些文档放入集合中,对它们执行查询,然后将结果分配
给一个局部变量(用
"var"
定义的变量就是局部变量)。在这里,先创建一个非常简单的集
合并对其进行查询,然后将结果存储在
cursor
变量中:
> for(i=0; i<100; i++) {
... db.collection.insertOne({x : i});
... }
> var cursor = db.collection.find();
这样做的好处是可以一次查看一个结果。如果将结果存储到全局变量中或根本不存储到变
量中,那么
MongoDB shell
将自动遍历并显示最开始的几个文档
。这是到目前为止我们一
直看到的种种例子,通常大家也只是希望看到集合中的内容,而不是使用
shell
进行编程。
要遍历结果,可以在游标上使用
next
方法。可以使用
hasNext
检查是否还有其他结果。典
型的结果遍历如下所示:
> while ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

大数据项目管理:从规划到实现

大数据项目管理:从规划到实现

Ted Malaska, Jonathan Seidman
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
Spark快速大数据分析(第2版)

Spark快速大数据分析(第2版)

Jules S. Damji, Brooke Wenig, Tathagata Das, Denny Lee
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9787115576538