MongoDB数据库
1. 数据查询
- ⽅法find(): 查询
db.集合名.find({document}) - ⽅法findOne():查询,只返回第⼀个
db.集合名.findOne({document}) - ⽅法pretty(): 将结果格式化
db.集合名.find({document}).pretty()
> db.stu.find()
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2 }
{ "_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"), "name" : "my", "age" : 24, "gender" : 2 }
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2 }
> db.stu.findOne()
{
"_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"),
"name" : "hx",
"age" : 22,
"gender" : 2
}
> db.stu.find().pretty()
{
"_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"),
"name" : "hx",
"age" : 22,
"gender" : 2
}
{
"_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"),
"name" : "my",
"age" : 24,
"gender" : 2
}
{
"_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"),
"name" : "xy",
"age" : 24,
"gender" : 2
}
2. 运算符
2.1 比较运算符
比较方式 | 运算符 | 意义 |
---|---|---|
等于 | None | 默认是等于判断, 没有运算符 |
⼩于 | $lt | less than |
⼩于等于 | $lte | less than equal |
⼤于 | $gt | greater than |
⼤于等于 | $gte | greater than equal |
不等于 | $ne | not equal |
例如:db.stu.find({age:{$gte:18}})
2.2 逻辑运算符
-
and:写多个条件即可
查询年龄⼤于或等于18, 并且性别为true的学⽣
db.stu.find({age:{$gte:18}, gender:true})
-
or:使⽤
$or
, 值为数组
查询年龄⼤于18,或性别为false的学⽣
db.stu.find({$or:[{age:{$gt:18}}, {gender:false}]})
连用:
查询年龄⼤于18或性别为男⽣, 并且姓名是xy
db.stu.find({$or:[{age:{$gte:18}}, {gender:true}], name:'xy'})
2.3 范围运算符
- 使⽤
$in
,$nin
查询指定范围的内容
字段:{$in: [a, b, c]}
查询含有a, b, c的指定字段
字段:{$nin: [a, b, c]}
查询不含有a, b, c的指定字段
例如:
查询年龄为22、 24的学⽣
db.stu.find({age:{$in:[22, 24]}})
> db.stu.find({age:{$in:[22, 24]}})
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"), "name" : "my", "age" : 24, "gender" : 2, "hometown" : "wf" }
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2, "hometown" : "qd" }
> db.stu.find({age:{$nin:[24]}})
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dac0dddf29e62ebc28cc5f6"), "name" : "xz", "age" : 23, "gender" : 2, "hometown" : "rz" }
{ "_id" : ObjectId("5dac0e04f29e62ebc28cc5f7"), "name" : "yh", "age" : 23, "gender" : 1, "hometown" : "qd" }
3. limit和skip
-
⽅法limit(): 用于读取指定数量的⽂档
db.集合名称.find().limit(N)
查询2条学⽣信息
db.stu.find().limit(2)
-
⽅法skip(): 用于跳过指定数量的⽂档
db.集合名称.find().skip(N)
跳过2条学⽣信息
db.stu.find().skip(2)
同时使用
db.stu.find().skip(2).limit(2)
> db.stu.find()
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"), "name" : "my", "age" : 24, "gender" : 2, "hometown" : "wf" }
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dac0dddf29e62ebc28cc5f6"), "name" : "xz", "age" : 23, "gender" : 2, "hometown" : "rz" }
{ "_id" : ObjectId("5dac0e04f29e62ebc28cc5f7"), "name" : "yh", "age" : 23, "gender" : 1, "hometown" : "qd" }
> db.stu.find().skip(2).limit(2)
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dac0dddf29e62ebc28cc5f6"), "name" : "xz", "age" : 23, "gender" : 2, "hometown" : "rz" }
4. 投影
- 在查询到的返回结果中, 只显示必要的字段
db.集合名称.find({}, {字段名:1,...})
参数为字段与值, 值为1表示显示
特殊: 对于_id列默认是显示的, 如果不显示需要明确设置为0
例如:
db.stu.find({}, {_id:0, name:1, hometown:1})
> db.stu.find({}, {_id:0, name:1, hometown:1})
{ "name" : "hx", "hometown" : "qd" }
{ "name" : "my", "hometown" : "wf" }
{ "name" : "xy", "hometown" : "qd" }
{ "name" : "xz", "hometown" : "rz" }
{ "name" : "yh", "hometown" : "qd" }
5. 排序
- ⽅法sort():用于排序
db.集合名称.find().sort({字段名:1,...})
参数1为升序排列
参数-1为降序排列
例如:
根据性别升序, 再根据年龄降序
db.stu.find().sort({gender:1, age:-1})
> db.stu.find()
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"), "name" : "my", "age" : 24, "gender" : 2, "hometown" : "wf" }
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dac0dddf29e62ebc28cc5f6"), "name" : "xz", "age" : 23, "gender" : 2, "hometown" : "rz" }
{ "_id" : ObjectId("5dac0e04f29e62ebc28cc5f7"), "name" : "yh", "age" : 23, "gender" : 1, "hometown" : "qd" }
> db.stu.find().sort({gender:1, age:-1})
{ "_id" : ObjectId("5dac0e04f29e62ebc28cc5f7"), "name" : "yh", "age" : 23, "gender" : 1, "hometown" : "qd" }
{ "_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"), "name" : "my", "age" : 24, "gender" : 2, "hometown" : "wf" }
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dac0dddf29e62ebc28cc5f6"), "name" : "xz", "age" : 23, "gender" : 2, "hometown" : "rz" }
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2, "hometown" : "qd" }
6. 统计个数
- ⽅法count():⽤于统计结果集中⽂档条数
db.集合名称.find({条件}).count()
db.集合名称.count({条件})
例如:
db.stu.find({gender:true}).count()
db.stu.count({age:{$gt:20}, gender:true})
> db.stu.find()
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"), "name" : "my", "age" : 24, "gender" : 2, "hometown" : "wf" }
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2, "hometown" : "qd" }
> db.stu.find({gender:2}).count()
3
> db.stu.count({age:{$gt:22}, gender:2})
2
7. 消除重复
- ⽅法distinct():对数据进⾏去重
db.集合名称.distinct('去重字段', {条件})
例如:
db.stu.distinct('hometown', {age:{$gt:18}})
> db.stu.find()
{ "_id" : ObjectId("5dabdf0cf29e62ebc28cc5f2"), "name" : "hx", "age" : 22, "gender" : 2, "hometown" : "qd" }
{ "_id" : ObjectId("5dabdf0ff29e62ebc28cc5f3"), "name" : "my", "age" : 24, "gender" : 2, "hometown" : "wf" }
{ "_id" : ObjectId("5dabdf80f29e62ebc28cc5f4"), "name" : "xy", "age" : 24, "gender" : 2, "hometown" : "qd" }
> db.stu.distinct('hometown', {age:{$gt:18}})
[ "qd", "wf" ]