当前位置:网站首页>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':/海$/}
)
边栏推荐
- GNNLab: A Novel GNN System Based on Spatial Sharing Ideas
- 掌握JESD204B(二)–AD6676的调试
- wsl2设置静态ip(固定ip)static ip
- THREEJS导入外部OBJ+MTL后内存优化
- DADPS-生物素-炔基_CAS:2241685-22-1试剂反应原理
- 掌握JESD204B(一)–AD6676的调试
- 测试第一题
- Vineyard: An open source distributed in-memory data management framework
- Deep Interpretation of void (*signal(int , void(*)(int)))(int) in "C Traps and Defects"
- 替换xxx.jar的class文件命令
猜你喜欢
网络协议03 - 路由和NAT
I can't hide it, I want to expose the bad things about cloud native
Biotin-PEG4-SS-Alkyne_1260247-54-8_生物素-PEG4-SS-炔烃
阿里京东“喜提”国电投,顶流互联网和能源大厂为何合作?
事件传递和响应者链条
图计算在网络安全分析中的应用
kubernetes搭建SonarQube进行代码扫描
关于memcache内核,全网最通俗的讲解
A New Paradigm for Distributed Deep Learning Programming: Global Tensor
单片机之流水灯
随机推荐
Unity Shader标准光照模型——高光反射
阿里京东“喜提”国电投,顶流互联网和能源大厂为何合作?
xxx is not in the sudoers file.This incident will be reported error
mysql数据库怎么设置手动提交
图扑数字孪生青岛城轨,赋能智慧交通低碳发展
网络协议01 - 基础概念
一种分布式深度学习编程新范式:Global Tensor
ParseException line 8:13 mismatched input ‘(‘ expecting ) near ‘int‘ in create table statement
读取GZ包的内容代码
Application of graph computing in network security analysis
事件传递和响应者链条
瀑布流(自定义布局实现)
Graph analysis like NetworkX with GraphScope
IO进程线程->文件IO->day2
独立按键控制led进阶(1)
用 GraphScope 像 NetworkX 一样做图分析
wsl2设置静态ip(固定ip)static ip
Knowledge of the day: handwritten deep copy and shallow copy (solves the problem of circular references)
libgrape-lite on GPUs:GPU助力加速图分析任务
使用 Helm 部署 GraphScope