实验目的:
(1)掌握MongoDB中数据查询的方法;
(2)掌握MongoDB中索引及其创建;
实验内容:
一、 MongoDB中数据查询的方法;
(1)find函数的使用;
(2)条件操作符: a l l 匹 配 所 有 、 all匹配所有、 all匹配所有、exists判断字段是否存在、null值处理、$mod取模运算、不等于、包含、不包含、数组元素个数、限制返回、排序、分页、随机显示。
(3)distinct找出给定键所有不同的值;
(4)group分组;
(5)游标;
(6)存储过程。
二、 MongoDB中索引及其创建;
(1)基础索引;
(2)文档索引;
(3)组合索引;
(4)唯一索引;
(5)强制使用索引;
(6)扩展索引;
(7)删除索引
(8)explain执行计划
一、MongoDB中数据查询的方法
(1). find函数的使用
db.student.find() 查询所有数据
(2). 条件操作符
$all匹配所有、
db.student.find({sex:{$all:['male']}})
查询男生的学生数据
$exists判断字段是否存在
> db.student.find({class: {$exists:true}})
查询所有拥有class字段的学生信息;如果exists:false
,表示不存在这个字段的信息。
null值处理
teacher集合中,_id=1的数据没有字段age,或者说age字段对应的值是null,所以可以进行null值处理,将他查出来。
db.teacher.find({age:null})
$mod取模运算
例如:找出teacher集合中年龄age对10取模等于1的人的数据。
db.teacher.find({age:{$mod:[10,1]}})
运算符:
- $gt 大于
- $gte 大于等于
- $lt 小于
- $lte 小于等于
- $ne 不等于
- $in 包含
- $nin 不包含
- $eq 等于
查询学生年龄大于22的数据
db.student.find({age:{$gt:22}})
查询学生年龄小于22的数据
db.student.find({age:{$lt:22}})
查询学生年龄等于22的数据
db.student.find({age:{$eq:22}})
数组元素个数
db.student.count()
#查出student集合中的记录数
db.student.find