目录初始mongoDBMonGoDB特点1、查询所有记录2、查询去掉后的当前聚集集合中的某列的重复数据3、查询 age = 22 的记录4、查询 age > 22 的记录5、查询 age < 22 的记录6、
MongoDB 是一个基于分布式文件存储的数据库。由 c++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
db.userInfo.find();
相当于:
select* from userInfo;
默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”
但是你可以设置每页显示数据的大小,用DBQuery.shellBATchSize= 50;这样每页就显示50条记录了。
db.userInfo.distinct("name");
会过滤掉 name 中的相同数据
相当于:
select distict name from userInfo;
db.userInfo.find({"age": 22});
相当于:
select * from userInfo where age = 22;
db.userInfo.find({age: {$gt: 22}});
相当于:
select * from userInfo where age > 22;
db.userInfo.find({age: {$lt: 22}});
相当于:
select * from userInfo where age < 22;
db.userInfo.find({age: {$gte: 25}});
相当于:
select * from userInfo where age >= 25;
db.userInfo.find({age: {$lte: 25}});
相当于:
select * from userInfo where age <= 25;
db.userInfo.find({age: {$gte: 23, $lte: 25}});
相当于:
select * from userInfo where age>=23 and age <= 25;
db.userInfo.find({age: {$ne: 25}});
相当于:
select * from userInfo where age != 25;
db.userInfo.find({name: /mongo/});
相当于:
select * from userInfo where name like '%mongo%';
db.userInfo.find({name: /^mongo/});
相当于:
select * from userInfo where name like 'mongo%';
db.userInfo.find({name: /mongo$/});
相当于:
select * from userInfo where name like ‘%mongo';
模糊查询语法:{ : /pattern/ }
其中options值可以为:
db.userInfo.find({}, {name: 1, age: 1});
相当于:
select name, age from userInfo;
当然 name 也可以用 true 或 false,当用 ture 的情况下和 name:1 效果一样,如果用 false 就是排除 name,显示 name 以外的列信息。
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:
select name, age from userInfo where age > 25;
升序:
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;
db.userInfo.find().limit(5);
相当于:
select * from userInfo limit 5;
db.userInfo.find().skip(10);
db.userInfo.find().limit(10).skip(5);
可用于分页,limit 是 pageSize,第n页时 skip 是 (n-1)*pageSize
相当于:
select * from userInfo limit 5,5;
db.userInfo.find({name: 'zhangsan', age: 22});
相当于:
select * from userInfo where name = 'zhangsan' and age = 22;
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:
select * from userInfo where age = 22 or age = 25;
注意多条件间用中括号[]包围。
db.userInfo.find({age :{$in:[22,25]}});
相当于:
select * from userInfo where age in (22,25);
统计数量
db.userInfo.find({age: {$gte: 25}}).count();
相当于:
select count(*) from userInfo where age >= 20;
skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是显示的 limit()。
db.userInfo.find({createTime:{$gt:ISODate("2020-11-09T00:00:00Z")}});
相当于:
select * from userInfo where createTime> '2020-11-09 00:00:00';
db.userInfo.aggregate({$group:{_id:null,score:{$sum:"$score"}}})
相当于:
SELECT SUM(score) from userInfo;
db.userInfo.aggregate({$group:{_id:null,score:{$avg:"$score"}}})
相当于:
SELECT AVG(score) from userInfo;
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';
假设 有 用户集合, 存储的测试数据 如下:
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常用查询语句内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
--结束END--
本文标题: MongoDB数据库常用28条查询语句总结
本文链接: https://www.lsjlt.com/news/211551.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-10
2024-05-10
2024-05-10
2024-05-10
2024-05-10
2024-05-10
2024-05-10
2024-05-10
2024-05-10
2024-05-10
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0