初始mongodb
mongodb 是一个基于分布式文件存储的数据库。由 c++ 语言编写。旨在为 web 应用提供可扩展的高性能数据存储解决方案。
mongodb 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
- database:数据库,sql中的database含义。
- collection:数据库表/集合,sql中的table含义。
- document:数据记录行/文档,sql中的row含义。
- field:数据字段/域,sql中的column含义。
- index:索引,sql中的index含义。
- primary key:主键,mongodb自动将_id字段设置为主键,sql中的primary key含义。
mongodb特点
- mongodb 是一个面向文档存储的数据库,操作起来比较简单和容易。
- 你可以在mongodb记录中设置任何属性的索引 (如:firstname="sameer",address="8 gandhi road")来实现更快的排序。
- 你可以通过本地或者网络创建数据镜像,这使得mongodb有更强的扩展性。
- 如果负载的增加(需要更多的存储空间和更强的处理能力) ,它可以分布在计算机网络中的其他节点上这就是所谓的分片。
- mongo支持丰富的查询表达式。查询指令使用json形式的标记,可轻易查询文档中内嵌的对象及数组。
- mongodb 使用update()命令可以实现替换完成的文档(数据)或者一些指定的数据字段 。
- mongodb中的map/reduce主要是用来对数据进行批量处理和聚合操作。
- map和reduce。map函数调用emit(key,value)遍历集合中所有的记录,将key与value传给reduce函数进行处理。
- map函数和reduce函数是使用javascript编写的,并可以通过db.runcommand或mapreduce命令来执行mapreduce操作。
- gridfs是mongodb中的一个内置功能,可以用于存放大量小文件。
- mongodb允许在服务端执行脚本,可以用javascript编写某个函数,直接在服务端执行,也可以把函数的定义存储在服务端,下次直接调用即可。
- mongodb支持各种编程语言:ruby,python,java,c++,php,c#等多种语言。
- mongodb安装简单。
1、查询所有记录
db.userinfo.find();
相当于:
select* from userinfo;
默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”
但是你可以设置每页显示数据的大小,用dbquery.shellbatchsize= 50;这样每页就显示50条记录了。
2、查询去掉后的当前聚集集合中的某列的重复数据
db.userinfo.distinct("name");
会过滤掉 name 中的相同数据
相当于:
select distict name from userinfo;
3、查询 age = 22 的记录
db.userinfo.find({"age": 22});
相当于:
select * from userinfo where age = 22;
4、查询 age > 22 的记录
db.userinfo.find({age: {$gt: 22}});
相当于:
select * from userinfo where age > 22;
5、查询 age < 22 的记录
db.userinfo.find({age: {$lt: 22}});
相当于:
select * from userinfo where age < 22;
6、查询 age >= 25 的记录
db.userinfo.find({age: {$gte: 25}});
相当于:
select * from userinfo where age >= 25;
7、查询 age <= 25 的记录
db.userinfo.find({age: {$lte: 25}});
相当于:
select * from userinfo where age <= 25;
8、查询 age >= 23 并且 age <= 25 注意书写格式
db.userinfo.find({age: {$gte: 23, $lte: 25}});
相当于:
select * from userinfo where age>=23 and age <= 25;
9、查询 age != 25 的记录
db.userinfo.find({age: {$ne: 25}});
相当于:
select * from userinfo where age != 25;
10、查询 name 中包含 mongo 的数据 模糊查询用于搜索
db.userinfo.find({name: /mongo/});
相当于:
select * from userinfo where name like '%mongo%';
11、查询 name 中以 mongo 开头的
db.userinfo.find({name: /^mongo/});
相当于:
select * from userinfo where name like 'mongo%';
12、查询 name 中以 mongo 结尾的
db.userinfo.find({name: /mongo$/});
相当于:
select * from userinfo where name like ‘%mongo';
模糊查询语法:{ : /pattern/ }
其中options值可以为:
- i -- 不区分大小写。
- m -- 匹配value中有换行符(\n)的情形,还有一个情形是:匹配规则中使用了锚,所谓的锚就是^ 开头, $ 结尾。
- s -- 允许点字符(.)匹配所有的字符,包括换行符。
- x -- 忽视所有空白字符。
13、查询指定列 name、age 数据
db.userinfo.find({}, {name: 1, age: 1});
相当于:
select name, age from userinfo;
当然 name 也可以用 true 或 false,当用 ture 的情况下和 name:1 效果一样,如果用 false 就是排除 name,显示 name 以外的列信息。
14、查询指定列 name、age 数据, age > 25
db.userinfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:
select name, age from userinfo where age > 25;
15、按照年龄排序 1 升序 -1 降序
升序:
db.userinfo.find().sort({age: 1});
相当于:
select * from userinfo order by age asc;
降序:
db.userinfo.find().sort({age: -1});
相当于:
select * from userinfo order by age desc;
17、查询前 5 条数据
db.userinfo.find().limit(5);
相当于:
select * from userinfo limit 5;
18、查询 10 条以后的数据
db.userinfo.find().skip(10);
19、查询在 6-10条 之间的数据
db.userinfo.find().limit(10).skip(5);
可用于分页,limit 是 pagesize,第n页时 skip 是 (n-1)*pagesize
相当于:
select * from userinfo limit 5,5;
20、and 查询 name = zhangsan, age = 22 的数据
db.userinfo.find({name: 'zhangsan', age: 22});
相当于:
select * from userinfo where name = 'zhangsan' and age = 22;
21、or 查询
db.userinfo.find({$or: [{age: 22}, {age: 25}]});
相当于:
select * from userinfo where age = 22 or age = 25;
注意多条件间用中括号[]包围。
22、in 查询
db.userinfo.find({age :{$in:[22,25]}});
相当于:
select * from userinfo where age in (22,25);
23、查询某个结果集的记录条数
统计数量
db.userinfo.find({age: {$gte: 25}}).count();
相当于:
select count(*) from userinfo where age >= 20;
skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是显示的 limit()。
24、查询某个时间段的数据(时间为日期类型,非字符串类型)
db.userinfo.find({createtime:{$gt:isodate("2020-11-09t00:00:00z")}});
相当于:
select * from userinfo where createtime> '2020-11-09 00:00:00';
25、对表中一字段进行统计求和
db.userinfo.aggregate({$group:{_id:null,score:{$sum:"$score"}}})
相当于:
select sum(score) from userinfo;
26、对表中一字段进行统计求平均值
db.userinfo.aggregate({$group:{_id:null,score:{$avg:"$score"}}})
相当于:
select avg(score) from userinfo;
27、对表中指定条件记录中的某字段求和
db.userinfo.aggregate({$match:{createtime:{$gte:isodate("2020-11-10t00:00:00z"),$lt:isodate("2020-11-11t00:00:00z")}}},{$group:{_id:null,score:{$sum:"$score"}}})
相当于:
select sum(score) from userinfo where createtime >= '2020-11-10 00:00:00' and createtime < '2020-11-11 00:00:00';
28、根据a表,匹配b表所有满足条件的集合,如根据用户表userinfo表中的userid字段找出useradress表中所有地址的集合,其中userid也为useradress中的字段。
假设 有 用户集合, 存储的测试数据 如下:
db.userinfo.insert([ { "_id" : 1, "userid" : "xxxx", "username" : "ruink", "website" : "www.51ste.com" }, { "_id" : 2, "userid" : "yyyy", "username" : "foosingy", "website" : "www.scgossip.com" } ])
假设 有 地址集合, 存储的测试数据 如下:
db.useradress.insert([ { "_id" : 1, "userid" : "xxxx", address: "测试地址1"}, { "_id" : 2, "userid" : "yyyy", address: "测试地址2"}, { "_id" : 3, "userid" : "xxxx", address: "测试地址3"}, ])
查询语句:
db.userinfo.aggregate([ { $lookup: { from: "useradress", localfield: "userid", foreignfield: "userid", as: "address_detail" } }, { $match : {"userid" :"xxxx"} } ])
上表为找出userid="xxxx"的所有地址的集合,查询结果如下:
[ { _id: 1, userid: 'xxxx', username: 'ruink', website:'www.51ste.com', address_docs: [ { _id: 1, userid: 'xxxx', address: '测试地址1' }, { _id: 3, userid: 'xxxx', address: '测试地址3' } ] } ]
总结
到此这篇关于mongodb数据库常用28条查询语句总结的文章就介绍到这了,更多相关mongodb常用查询语句内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论