MongoDB数据的查询
数据的查询
若要从集合中选择文档,可以使用 find()
或者findOne()
方法。若要选择集合中的所有文档,请将空文档作为查询筛选器文档传递给该方法。
编写语法为
db.集合名.函数名()
函数名 | 含义 |
---|---|
find( <{条件文档}> ) |
查找到所有匹配数据 |
findOne( <{条件文档}> ) |
只返回匹配的第一个数据 |
运算符
语法 | 操作 | 格式 |
---|---|---|
$eq | 等于 | {:} |
$lt | 小于 | {:{$lt:}} |
$lte | 小于或等于 | {:{$lte:}} |
$gt | 大于 | {:{$gt:}} |
$gte | 大于或等于 | {:{$gte:}} |
$ne | 不等于 | {:{$ne:}} |
$or | 或 | {$or:[{},{}]} |
$in | 在范围内 | {age:{$in:[val1,val2]}} |
$nin | 不在范围内 | {age:{$nin:[val1,val2]}} |
举例
db.person.find({age:{$gt:16}})
db.person.find({$or:[{age:{$gte:18}},{name:"zs"}])
模糊匹配
使用//或$regex编写正则表达式
db.person.find({name:/^zs/})
db.person.find({name:{$regex:'^zs'}}})
自定义查询
使用$where后面写一个函数,返回满足条件的数据
db.person.find({$where:function(){return this.age>20}}) # 5.0版本不能用了
db.person.find({$where:"this.age==23"});
db.person.find("this.age >23");
db.person.find('this.country=="吴国" || this.age==23');
limit
用于读取指定数量的文档
db.集合名称.find().limit(NUMBER)
skip
用于跳过指定数量的文档
db.集合名称.find().skip(2)
sort
用于对结果集进行排序
db.集合名称.find().sort({字段:1,...})
- 参数1为升序排列
- 参数-1为降序排列
count
用于统计结果集中文档条数
db.集合名称.find({条件}).count()
db.集合名称.count({条件})
db.stu.find({gender:true}).count()
db.stu.count({age:{$gt:20},gender:true})
$exists
判断是否有某个字段
db.集合名称.find({'field':{$exists:true}})
dictinct
去重
db.集合名称.distinct(field)
db.集合名称.distinct(field,{过滤条件 })
样例数据
首先插入一批文档,再进行查询
db.person.insert([{"name":"司马懿","country":"魏国","age":35},
{"name":"张辽","country":"魏国","age":34},
{"name":"徐晃","country":"魏国","age":24},
{"name":"夏侯惇","country":"魏国","age":23},
{"name":"夏侯渊","country":"魏国","age":23},
{"name":"庞德","country":"魏国","age":23},
{"name":"张郃","country":"魏国","age":34},
{"name":"李典","country":"魏国","age":41},
{"name":"乐进","country":"魏国","age":34},
{"name":"典韦","country":"魏国","age":12},
{"name":"曹洪","country":"魏国","age":21},
{"name":"曹仁","country":"魏国","age":11},
{"name":"诸葛亮","country":"蜀国","age":20},
{"name":"关羽","country":"蜀国","age":32},
{"name":"张飞","country":"蜀国","age":23},
{"name":"马超","country":"蜀国","age":53},
{"name":"黄忠","country":"蜀国","age":23},
{"name":"赵云","country":"蜀国","age":32},
{"name":"魏延","country":"蜀国","age":42},
{"name":"关平","country":"蜀国","age":12},
{"name":"周仓","country":"蜀国","age":42},
{"name":"关兴","country":"蜀国","age":23},
{"name":"张苞","country":"蜀国","age":12},
{"name":"周瑜","country":"吴国","age":32},
{"name":"吕蒙","country":"吴国","age":11},
{"name":"甘宁","country":"吴国","age":23},
{"name":"太史慈","country":"吴国","age":23},
{"name":"程普","country":"吴国","age":24},
{"name":"黄盖","country":"吴国","age":28},
{"name":"韩当","country":"吴国","age":23},
{"name":"周泰","country":"吴国","age":29},
{"name":"蒋钦","country":"吴国","age":19},
{"name":"丁奉","country":"吴国","age":17},
{"name":"徐盛","country":"吴国","age":27}
])
//获取所有的数据
db.person.find();
//获取一个数据
db.person.findOne();
//获取指定数据
db.person.find({'age':20});
db.person.find({'country':'蜀国'});
db.person.find({'age':{$gt:40}});
db.person.find({$or:[{"age":23},{"country":"吴国"}]});
db.person.find({'age':{$in:[19,29,24]}});
//模糊查询
db.person.find({'name':/^曹/});
db.person.find({'name':{$regex:'^曹'}});
//自定义查询
db.person.find({$where:'this.age>23'});
//limit限定数量
db.person.find().limit(5);
//跳过去数量,skip
db.person.find().skip(30);
db.person.find().skip(20).limit(5);
//排序sort
db.person.find().sort({'age':1});
db.person.find().sort({'age':1,'name':-1});
//当前有多少文档
db.person.find().count();
//拥有某个字段
db.person.find({'age':{$exists:true}});
//去重
db.person.distinct('age');