当前位置:网站首页>MongoDB-查询
MongoDB-查询
2022-07-30 05:48:00 【HDLaZy】
1:查询
查询集合中所有文档:
db.集合名.find()
对查询结果进行去重:
db.集合名.distinct('指定Key')
查询指定字段:
db.集合名.find(
{},#查询条件,类似于where
{
'filed1':true,'filed2':false}#设置返回的字段,类似于select,true为返回,false为不返回,可以使用0代表false,1代表true
)
2:运算符
| 运算符 | 对应 |
|---|---|
| > | $gt |
| < | $lt |
| >= | $gte |
| <= | $lte |
| != | $ne |
#查询年纪在16-19之间,班级不为二班的学生
db.student.find(
{
'age':{
'$gte':16,'$lte':19},'class_name':{
'$ne':'二班'}},
{
'_id',0,'name':1,'class_name':1}
)
and和or:
#查询班级为二班,并且年纪大于17的学生
db.student.find(
{
'class_name':'二班','age':{
'$gt':17}}
)
#查询班级为二班或者三班的学生
db.student.find(
{
'$or':[{
'class_name':'二班'},{
'class_name':'三班'}]}
)
3:count/limit
count:返回符合结果的个数
#查看三班男生的人数
db.studnet.fine(
{
'class_name':'三班','sex':'男'}
).count()
limit:设置查询结果返回数
#查询三班年纪大于22的三个人
db.student.find(
{
'class_name':'三班','age':{
'$get':22}}
).limit(3)
spik:设置从下标为几开始返回,起始下标为0,默认为0
#查询三班的人,从第三条文档开始返回4条
#返回的下标2,3,4,5
#实际文档序号3,4,5,6
db.student.find(
{
'class_name':'三班'}
).limit(4).skip(2)
4:排序查询结果
sort:对查询结果按照某一个key进行排序
参数1表示升序
参数-1表示降序
#返回三班年纪最大的人的年纪
db.studnet.find(
{
'class_name':'三班'},
{
'age':1,'_id':0}
).sort({
'age':-1}).limit(1)
5:分组和聚合函数
group:max,min,avg,sum
参数_id:表示分组的字段
#查询各个班级年纪最大的人
db.student.aggregate(
{
'$group':{
'_id':'class_name','最大年纪':{
'$max':'$age'}}}
)
6:模糊查询
- //包含
- /^/开头
- /$/结尾
#名字包含李的
db.student.find(
{
'name':/李/}
)
#以王开头的
db.student.find(
{
'name':/^王/}
)
#以海结尾的
db.student.find(
{
'name':/海$/}
)
边栏推荐
- Delete all files containing a keyword in the current path
- 04-加壳和脱壳
- A New Paradigm for Distributed Deep Learning Programming: Global Tensor
- GCD timer
- 一种分布式深度学习编程新范式:Global Tensor
- The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
- Alamofire source code analysis - POST request
- ES6 syntax notes (ES6~ES11)
- 如何将matlab数据导入modelsim仿真
- 掌握JESD204B(二)–AD6676的调试
猜你喜欢
随机推荐
IO进程线程->目录IO->day3
The Force Plan Microservices | Centralized Configuration Center Config Asymmetric Encryption and Security Management
Unity Shader的结构与语义
图计算101:图计算的类型、语言与系统
libgrape-lite on GPUs:GPU助力加速图分析任务
单向链表的操作(带头结点)
GadgetInspector原理分析
IO进程线程->文件IO->day2
基于粒子(Points)模拟雨雪天气效果
Jenkins一些常见问题
ParseException line 8:13 mismatched input ‘(‘ expecting ) near ‘int‘ in create table statement
实现二叉树--实现删除
Unity Shader标准光照模型——高光反射
How to open terminal in VsCode
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
基于STM32F103的消防系统之火焰传感器
洛谷一P1097 [NOIP2007 提高组] 统计数字
从 Vertex 到 Subgraph 再到 PIE: 并行图计算编程模型概览
【动态规划】LeetCode刷题清单及思路记录
ES6 syntax notes (ES6~ES11)









