- MongoDB官方文档中文版
 - MongoDB中文手册说明
 - MongoDB简介
 - 安装 MongoDB
 - The mongo Shell
 - MongoDB CRUD 操作
 - 聚合
 - 数据模型
 - 事务
 - 索引
 -  安全   
- 安全检查列表
 - 启用访问控制
 - 身份验证
 - 基于角色的访问控制
 - TLS / SSL(传输加密)
 - 静态加密
 - 客户端字段级加密
 - 审计
 - 网络和配置强化
 - 实现字段级别修订
 - 安全参考
 - 附录
 
 - 变更流
 - 复制
 - 分片   
- 分片键
 - 哈希分片
 - 范围分片
 - 区
 - 管理分片区
 - 按位置细分数据
 - 用于更改SLA或SLO的分层硬件
 - 按应用或客户细分数据
 - 仅插入工作负载的分布式本地写入
 - 管理分片区
 - 使用块进行数据分区
 - 在分片集群中拆分数据块
 
 - 管理
 -  存储   
-  存储引擎   
- 日志记录
 - 管理日志记录   
- GridFS
 
 
 - FAQ:MongoDB 存储
 
 -  存储引擎   
 - 参考   
- 运算符
 - 查询与映射运算符
 - 更新运算符
 - 聚合管道阶段
 -  聚合管道操作符   
- $abs (aggregation)
 - $acos (aggregation)
 - $acosh (aggregation)
 - $add (aggregation)
 - $addToSet (aggregation)
 - $allElementsTrue (aggregation)
 - $and (aggregation)
 - $anyElementTrue (aggregation)
 - $arrayElemAt (aggregation)
 - $arrayToObject (aggregation)
 - $asin (aggregation)
 - $asinh (aggregation)
 - $atan (aggregation)
 - $atan2 (aggregation)
 - $atanh (aggregation)
 - $avg (aggregation)
 - $ceil (aggregation)
 - $cmp (aggregation)
 - $concat (aggregation)
 - $concatArrays (aggregation)
 - $cond (aggregation)
 - $convert (aggregation)
 - $cos (aggregation)
 - $dateFromParts (aggregation)
 - $dateToParts (aggregation)
 - $dateFromString (aggregation)
 - $literal (aggregation)
 
 - 查询修饰符
 - 数据库命令
 - 聚合命令
 - 地理空间命令
 - 查询和写操作命令
 - 查询计划缓存命令
 - 认证命令
 - 用户管理命令
 - 角色管理命令
 - 复制命令
 - 分片命令
 - 会话命令
 - 管理命令
 - 诊断命令
 - 免费监控命令
 - 系统事件审计命令
 - mongo Shell 方法
 -  集合方法   
- db.collection.aggregate()
 - db.collection.bulkWrite()
 - db.collection.copyTo()
 - db.collection.count()
 - db.collection.countDocuments()
 - db.collection.estimatedDocumentCount()
 - db.collection.createIndex()
 - db.collection.createIndexes()
 - db.collection.dataSize()
 - db.collection.deleteOne()
 - db.collection.deleteMany()
 - db.collection.distinct()
 - db.collection.drop()
 - db.collection.dropIndex()
 - db.collection.dropIndexes()
 - db.collection.ensureIndex()
 - db.collection.explain()
 - db.collection.find()
 - db.collection.findAndModify()
 - db.collection.findOne()
 - db.collection.findOneAndDelete()
 - db.collection.findOneAndReplace()
 - db.collection.findOneAndUpdate()
 - db.collection.getIndexes()
 - db.collection.getShardDistribution()
 - db.collection.getShardVersion()
 - db.collection.insert()
 - db.collection.insertOne()
 - db.collection.insertMany()
 - db.collection.isCapped()
 - db.collection.latencyStats()
 - db.collection.mapReduce()
 - db.collection.reIndex()
 - db.collection.remove()
 - db.collection.renameCollection()
 - db.collection.replaceOne()
 - db.collection.save()
 - db.collection.stats()
 - db.collection.storageSize()
 - db.collection.totalIndexSize()
 - db.collection.totalSize()
 - db.collection.update()
 - db.collection.updateOne()
 - db.collection.updateMany()
 - db.collection.watch()
 - db.collection.validate()
 
 - 词汇表
 - 默认的MongoDB端口
 - 默认的MongoDB读/写关注
 - 服务器会话
 
 - MongoDB驱动
 - FAQ
 - 联系我们
 - 更多资料   
- [快学Mongo]
 - [Mongo问题讨论区]
 - [Mongo 驱动使用手册]
 
 - 本书使用 GitBook 发布
 
查询文档
查询文档
这个页面提供了使用mongo shell中的db.collection.find()方法的查询操作示例。此页上的示例使用inventory集合。要填充inventory集合,请运行以下操作:
db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
选择集合中的所有文档
要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给find方法。 查询过滤器参数确定选择条件:
db.inventory.find( {} )
此操作对应于以下SQL语句:
SELECT * FROM inventory
有关该方法的语法的更多信息,请参见find()。
指定平等条件
要指定相等条件,请在查询筛选文档使用<field>:<value>表达式:
{ <field1>: <value1>, ... }
下面的示例从inventory中选择状态等于" D"的所有文档:
db.inventory.find( { status: "D" } )
此操作对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "D"
使用查询运算符指定条件
查询过滤器文档可以使用查询运算符以以下形式指定条件:
{ <field1>: { <operator1>: <value1> }, ... }
下面的例子从状态等于" A"或" D"的inventory集合中检索所有文档:
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
[success] Note
该操作对应于以下SQL语句:
SELECT * FROM inventory WHERE status in ("A", "D")
有关MongoDB查询运算符的完整列表,请参阅查询和投影运算符文档。
指定和条件
复合查询可以为集合文档中的多个字段指定条件。逻辑和连词隐式地连接复合查询的子句,以便查询在集合中选择符合所有条件的文档。
下面的示例 inventory 状态为"A"且数量小于($lt) 30的库存集合中的所有文档:
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
该操作对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "A" AND qty < 30
有关其他MongoDB比较运算符,请参阅比较运算符 。
指定或条件
使用$or操作符,可以指定一个复合查询,用逻辑OR连词连接每个子句,以便查询在集合中选择至少匹配一个条件的文档。
下面的示例retrieve状态为“A”或qty小于($lt)30的集合中的所有文档:
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
该操作对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "A" OR qty < 30
[success] Note
指定和以及或条件
在下面的例子中,复合查询文档选择状态为“A”且qty小于($lt) 30或item以字符p开头的集合中的所有文档:
db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
该操作对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
[success] Note
MongoDB支持正则表达式
$regex查询来执行字符串模式匹配。
附加查询教程
有关其他查询示例,请参见:
- Query on Embedded/Nested Documents
 - Query an Array
 - Query an Array of Embedded Documents
 - Project Fields to Return from Query
 - Query for Null or Missing Fields
 
行为
游标
db.collection.find()方法将游标 返回到匹配的文档。
读取隔离
3.2版中的新功能
对于复制集 和复制集分片的读取,读取关注允许客户端为其读取选择隔离级别。 有关更多信息,请参见阅读关注。
附加方法
以下方法也可以从集合中读取文档:
- db.collection.findOne
 - 在聚合管道中,
$match管道步骤提供对MongoDB查询的访问. 
[success] Note
db.collection.findOne()方法还执行读取操作以返回单个文档。在内部,db.collection.findOne()方法是db.collection.find()方法,限制为1。
译者:杨帅
校对:杨帅
Copyright © 上海锦木信息技术有限公司 all right reserved,由 MongoDB汉化小组 提供技术支持文件修订时间: 2020-10-11 20:53:05
