当前位置:网站首页>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

迷宫问题----经典回溯法解决

含 3 个单元 PEG 的 ADC linker的PC DBCO-PEG3-Biotin

libgrape-lite: 提供 GraphScope 的图分析能力

kubernetes搭建SonarQube进行代码扫描

Vineyard: An open source distributed in-memory data management framework

OP 代币和不可转让的 NFT 致力于建立新的数字民主

Unity Shader 标准光照模型——漫反射

04-加壳和脱壳

元宇宙与图扑国风的碰撞,科技与文化的虚实融合
随机推荐
Unity Shader 空间坐标系
GCD的定时器
单向链表的操作(带头结点)
How to import matlab data into modelsim simulation
Vineyard: An open source distributed in-memory data management framework
xxx is not in the sudoers file.This incident will be reported error
四、6、前缀、中缀、后缀表达式(逆波兰表达式)
测试第一题
网络协议01 - 基础概念
基于STM32F103的消防系统之MQ-4气体传感器
04-packing and unpacking
wsl2设置静态ip(固定ip)static ip
Knowledge of the day: handwritten deep copy and shallow copy (solves the problem of circular references)
图扑数字孪生煤矿开采系统,打造采煤“硬实力”
---------手撕二叉树,完成二叉树的前中后序遍历,以及前中后序查找
Biotin-SS-NHS ester|生物素-二硫键-活性酯|CAS:122266-55-1具有良好的水溶性
The Force Plan Microservices | Centralized Configuration Center Config Asymmetric Encryption and Security Management
如何将matlab数据导入modelsim仿真
THREEJS导入外部OBJ+MTL后内存优化
掌握JESD204B(二)–AD6676的调试