查询文档
bash
db.students.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" }
]
)1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
基本查询
db.col.findOne()
语法
javascript
db.collection.findOne( <query>, <projection>, <options> )1
参数
query:可选,使用查询运算符指定查询选择条件。如果为{}或空 则返回集合中返回的第一个文档。projection:指定要使用投影运算符返回的字段。省略此参数可返回匹配文档中的所有字段。options:指定查询的其他选项。这些选项可修改查询行为以及返回结果的方式。详细文档。
返回
- 返回数据,无数据则返回
null
查询
students的第一个文档javascriptdb.students.findOne()1查询
students中{item:'journal'}的文档javascriptdb.students.findOne({item:'journal'})1查询
students中{item:'journal'}的文档,且只返回sizejavascriptdb.students.findOne( {item:'journal'}, {'size':1, _id:0} )1
2
3
4
db.col.find()
语法
javascript
db.collection.find( <query>, <projection>, <options> )1
参数
query:可选,使用查询运算符指定查询选择条件。如果为{}或空 则返回集合中返回的第一个文档。projection:指定要使用投影运算符返回的字段。省略此参数可返回匹配文档中的所有字段。options:指定查询的其他选项。这些选项可修改查询行为以及返回结果的方式。详细文档。
返回
- 返回数据,无数据则什么都不返回
查询
students集合所有数据javascriptdb.students.find()1查询
students集合所有数据,仅需要sizejavascriptdb.students.find( {}, {'size':1, _id:0} )1
2
3
4
条件查询
排序查询
说明
$sort:排序
1:正序-1:倒序
查询所有数据,按照 qty 正序查询
javascript
db.students.aggregate([
{ $sort: {qty:1} }
])1
2
3
2
3
分页查询
提示
$skip: 跳过前面几条数据
$limit:限制返回的数据条数
算法
javascript
skip = (pageNumber - 1) * pageSize1
pageSize: 每页显示的数据条数pageNumber: 要查询的页数
查询前两条数据
javascriptdb.students.aggregate( [ { $limit:2 } ] )1
2
3
4
5每页显示两条,显示第二页信息
javascriptdb.students.aggregate( [ { $skip:2 }, { $limit:2 } ] )1
2
3
4
5
6根据
qty正序,每页显示两条,显示第二页数据javascriptdb.students.aggregate( [ { $sort: { qty: 1} }, { $skip: 2 }, { $limit: 2 } ] )1
2
3
4
5
6
7
计数
方法
db.col.countDocuments() 获取符合特定查询条件的文档数量。
计算 qty > 20 的文档数量
javascript
db.students.countDocuments({qty:{$gt:50}})1
< ~/ > MyNote