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版)
查询
47
键的值减
1
。这样,只需用一条简单的查询语句就能检查出哪些商品处于缺货状态了。
> db.stock.find({"in_stock" : 0})
4.2
 查询条件
查询不仅能像上一节描述的那样进行精确匹配,还可以匹配更加复杂的条件,比如范围、
OR
子句以及取反。
4.2.1
 查询条件
"$lt"
"$lte"
"$gt"
"$gte"
都属于比较运算符,分别对应
<
<=
>
>=
。可以将
它们组合使用以查找一个范围内的值。例如,要查询
18
30
岁的用户
,可以进行如下
操作:
> db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})
这样就可以查找到
"age"
字段大于等于
18
并且小于等于
30
的所有文档了。
这类范围查询通常对日期非常有用。例如,要查找在
2007
1
1
日前注册的用户
,可
以这样做:
> start = new Date("01/01/2007")
> db.users.find({"registered" : {"$lt" : start}})
根据创建和存储日期的方式,精确匹配可能用处不大,因为日期是以毫秒精度存储的。通
常,你需要的是一整天、一周或一个月的数据,因此需要使用范围查询。
对于文档键值不等于某个特定值的情况,就要使用另外一种条件运算符
"$ne"
了,它表示
“不相等”。如果想找到所有用户名不为
joe
的用户,可以像下面这样查询:
> db.users.find({"username" : {"$ne" : "joe"}})
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