MongoDB数据的查询
1.Scrapy_ImagePipeline保存图片2.Scrapy 保存数据案例-小说保存3.Scrapy 中 CrawlSpider 使用(一)4.Scrapy 中 CrawlSpider 使用(二)5.Scrapy 中 Request 的使用6.Scrapy_Request对象meta演示7.Scrapy_Request对象dont_filter演示8.Scrapy_Request对象Cookie的演示9.Scrapy_FormRequest对象的使用10.Scrapy中下载中间件11.Scrapy_下载中间件设置UserAgent12.Scrapy 中 Downloader 设置代理13.下载中间件实战-Scrapy与Selenium结合14.MongoDB介绍15.Mongo_如果快速学习Mongo16.Windows安装与启动MongoDB17.MongoDB通过配置文件管理参数18.Linux安装MongoDB19.什么是Docker20.为什么要使用Docker21.Docker核心概念22.Docker安装23.Docker核心命令_镜像命令24.Docker核心命令_容器命令25.Docker核心命令_其他命令26.Docker 安装 MongoDB27.MongoDB基础命令28.MongoDB数据的增加29.MongoDB数据的更新30.MongoDB删除文档
31.MongoDB数据的查询
32.Docker数据管理_为什么使用数据卷33.Docker数据管理_配置数据卷34.Docker数据管理_数据卷容器35.Docker实战_Mysql数据卷挂载36.MongoDB聚合操作之分组、过滤37.MongoDB聚合操作之排序、分页38.MongoDB索引Index39.Mongo中唯一索引\复合索引40.mongodb和python交互41.Splash反爬42.Dockerfile完全指南_什么是Dockerfile43.Dockerfile完全指南_构建镜像44.Dockerfile完全指南_常见的13种指令上45.Docker网络管理_Docker0网络详解46.Docker网络管理_四种网络模式47.Docker网络管理_自定义网络48.Scrapy保存数据到多个数据库49.Splash与requests结合50.Splash与Scrapy结合51.图片验证码介绍52.验证码-手动输入53.超级鹰识别验证码54.移动端爬虫55.移动端工具的安装56.模拟器配置57.Appium介绍58.Appium环境搭建 - Android SDK安装59.Appium环境搭建 - Appium安装60.appium实战61.滑动验证码-保存图片62.滑动验证码-获取滑动长度63.滑动验证码-编辑移动轨迹64.滑动验证码-移动滑块65.LOL皮肤66.Python调用 JS -PyExecJS, Python 调用 JS -js2py数据的查询
若要从集合中选择文档,可以使用 find()
或者findOne()
方法。若要选择集合中的所有文档,请将空文档作为查询筛选器文档传递给该方法。
编写语法为
db.集合名.函数名()
函数名 | 含义 |
---|---|
find( <{条件文档}> ) |
查找到所有匹配数据 |
findOne( <{条件文档}> ) |
只返回匹配的第一个数据 |
运算符
语法 | 操作 | 格式 |
---|---|---|
$eq | 等于 | {:} |
$lt | 小于 | {:{$lt:}} |
$lte | 小于或等于 | {:{$lte:}} |
$gt | 大于 | {:{$gt:}} |
$gte | 大于或等于 | {:{$gte:}} |
$ne | 不等于 | {:{$ne:}} |
$or | 或 | {$or:[{},{}]} |
$in | 在范围内 | {age:{$in:[val1,val2]}} |
$nin | 不在范围内 | {age:{$nin:[val1,val2]}} |
举例
db.person.find({age:{$gt:16}})
db.person.find({$or:[{age:{$gte:18}},{name:"zs"}])
模糊匹配
使用//或$regex编写正则表达式
db.person.find({name:/^zs/})
db.person.find({name:{$regex:'^zs'}}})
自定义查询
使用$where后面写一个函数,返回满足条件的数据
db.person.find({$where:function(){return this.age>20}}) # 5.0版本不能用了
db.person.find({$where:"this.age==23"});
db.person.find("this.age >23");
db.person.find('this.country=="吴国" || this.age==23');
limit
用于读取指定数量的文档
db.集合名称.find().limit(NUMBER)
skip
用于跳过指定数量的文档
db.集合名称.find().skip(2)
sort
用于对结果集进行排序
db.集合名称.find().sort({字段:1,...})
- 参数1为升序排列
- 参数-1为降序排列
count
用于统计结果集中文档条数
db.集合名称.find({条件}).count()
db.集合名称.count({条件})
db.stu.find({gender:true}).count()
db.stu.count({age:{$gt:20},gender:true})
$exists
判断是否有某个字段
db.集合名称.find({'field':{$exists:true}})
dictinct
去重
db.集合名称.distinct(field)
db.集合名称.distinct(field,{过滤条件 })
样例数据
首先插入一批文档,再进行查询
db.person.insert([{"name":"司马懿","country":"魏国","age":35},
{"name":"张辽","country":"魏国","age":34},
{"name":"徐晃","country":"魏国","age":24},
{"name":"夏侯惇","country":"魏国","age":23},
{"name":"夏侯渊","country":"魏国","age":23},
{"name":"庞德","country":"魏国","age":23},
{"name":"张郃","country":"魏国","age":34},
{"name":"李典","country":"魏国","age":41},
{"name":"乐进","country":"魏国","age":34},
{"name":"典韦","country":"魏国","age":12},
{"name":"曹洪","country":"魏国","age":21},
{"name":"曹仁","country":"魏国","age":11},
{"name":"诸葛亮","country":"蜀国","age":20},
{"name":"关羽","country":"蜀国","age":32},
{"name":"张飞","country":"蜀国","age":23},
{"name":"马超","country":"蜀国","age":53},
{"name":"黄忠","country":"蜀国","age":23},
{"name":"赵云","country":"蜀国","age":32},
{"name":"魏延","country":"蜀国","age":42},
{"name":"关平","country":"蜀国","age":12},
{"name":"周仓","country":"蜀国","age":42},
{"name":"关兴","country":"蜀国","age":23},
{"name":"张苞","country":"蜀国","age":12},
{"name":"周瑜","country":"吴国","age":32},
{"name":"吕蒙","country":"吴国","age":11},
{"name":"甘宁","country":"吴国","age":23},
{"name":"太史慈","country":"吴国","age":23},
{"name":"程普","country":"吴国","age":24},
{"name":"黄盖","country":"吴国","age":28},
{"name":"韩当","country":"吴国","age":23},
{"name":"周泰","country":"吴国","age":29},
{"name":"蒋钦","country":"吴国","age":19},
{"name":"丁奉","country":"吴国","age":17},
{"name":"徐盛","country":"吴国","age":27}
])
//获取所有的数据
db.person.find();
//获取一个数据
db.person.findOne();
//获取指定数据
db.person.find({'age':20});
db.person.find({'country':'蜀国'});
db.person.find({'age':{$gt:40}});
db.person.find({$or:[{"age":23},{"country":"吴国"}]});
db.person.find({'age':{$in:[19,29,24]}});
//模糊查询
db.person.find({'name':/^曹/});
db.person.find({'name':{$regex:'^曹'}});
//自定义查询
db.person.find({$where:'this.age>23'});
//limit限定数量
db.person.find().limit(5);
//跳过去数量,skip
db.person.find().skip(30);
db.person.find().skip(20).limit(5);
//排序sort
db.person.find().sort({'age':1});
db.person.find().sort({'age':1,'name':-1});
//当前有多少文档
db.person.find().count();
//拥有某个字段
db.person.find({'age':{$exists:true}});
//去重
db.person.distinct('age');
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库