首页/文章/mongodb

21-11-09mongodb查询语句

2021-11-09
2181 字约 8 分钟
...

1、查询所有记录 db.userInfo.find(); 相当于:select* from userInfo;

2、查询去掉后的当前聚集集合中的某列的重复数据 db.userInfo.distinct("name"); 会过滤掉 name 中的相同数据 相当于:select distict name from userInfo;

3、查询 age = 22 的记录 db.userInfo.find({"age": 22}); 相当于: select * from userInfo where age = 22;

4、查询 age > 22 的记录 db.userInfo.find({age: {$gt: 22}}); 相当于:select * from userInfo where age >22;

5、查询 age < 22 的记录 db.userInfo.find({age: {$lt: 22}}); 相当于:select * from userInfo where age <22;

6、查询 age >= 25 的记录 db.userInfo.find({age: {$gte: 25}}); 相当于:select * from userInfo where age >= 25;

7、查询 age <= 25 的记录 db.userInfo.find({age: {$lte: 25}});

8、查询 age >= 23 并且 age <= 26 注意书写格式 db.userInfo.find({age: {$gte: 23, $lte: 26}});

9、查询 name 中包含 mongo 的数据 模糊查询用于搜索 db.userInfo.find({name: /mongo/}); //相当于%% select * from userInfo where name like ‘%mongo%’;

10、查询 name 中以 mongo 开头的 db.userInfo.find({name: /^mongo/}); select * from userInfo where name like ‘mongo%’;

11、查询指定列 name、age 数据 db.userInfo.find({}, {name: 1, age: 1}); 相当于:select name, age from userInfo; 当然 name 也可以用 true 或 false,当用 ture 的情况下河 name:1 效果一样,如果用 false 就 是排除 name,显示 name 以外的列信息。

12、查询指定列 name、age 数据, age > 25 db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1}); 相当于:select name, age from userInfo where age >25;

13、按照年龄排序 1 升序 -1 降序 升序:db.userInfo.find().sort({age: 1}); 降序:db.userInfo.find().sort({age: -1});

14、查询 name = zhangsan, age = 22 的数据 db.userInfo.find({name: 'zhangsan', age: 22}); 相当于:select * from userInfo where name = ‘zhangsan’ and age = ‘22’;

15、查询前 5 条数据 db.userInfo.find().limit(5); 相当于:selecttop 5 * from userInfo;

16、查询 10 条以后的数据 db.userInfo.find().skip(10); 相当于:select * from userInfo where id not in ( selecttop 10 * from userInfo );

17、查询在 5-10 之间的数据 db.userInfo.find().limit(10).skip(5); 可用于分页,limit 是 pageSize,skip 是第几页*pageSize

18、or 与 查询 db.userInfo.find({$or: [{age: 22}, {age: 25}]}); 相当于:select * from userInfo where age = 22 or age = 25;

19、findOne 查询第一条数据 db.userInfo.findOne(); 相当于:selecttop 1 * from userInfo; db.userInfo.find().limit(1);

20、查询某个结果集的记录条数 统计数量 db.userInfo.find({age: {$gte: 25}}).count(); 相当于:select count(*) from userInfo where age >= 20; 如果要返回限制之后的记录数量,要使用 count(true)或者 count(非 0) db.users.find().skip(10).limit(5).count(true);

如果您觉得这篇文章有帮助,请点个赞吧~

分享文章

相关文章

更多文章 →
http-网络2026-10-15
OAuth 2.0是什么,以及如何正确接入
一、先从一个需求理解 OAuth 2.0 假设你开发了一个相册整理工具,用户希望它读取某个云盘里的照片。 把云盘密码交给工具,会带来几个问题:工具拿到的权限太大,密码可能泄漏,用户也难以只撤销这个工具的访问。 OAuth 2.0 提供一套 委托授权机制 :用户在云盘自己的页面确认授权,工具取得一个有权限范围和有效期的访问令牌,再用它调用云盘 API。用户的云盘密码不交给工具。 可以把它理解为:给工具一张“限定用途的通行证”。但令牌泄漏同...
学习
前端2026-10-15
前端动画全景指南
一、前端动画到底是什么 前端动画,就是让画面随时间、用户输入或页面状态连续变化。变化的对象可以是按钮的位置、弹窗的透明度、SVG 的线条、Canvas 里的粒子,也可以是三维场景中的镜头。 先把三个问题分开: | 问题 | 常见答案 | | | | | 画什么? | DOM/CSS、SVG、Canvas 2D、GPU 渲染的场景、预制动画素材 | | 怎样变化? | CSS 过渡/关键帧、Web Animations API、逐帧计算、...
学习
flutter2026-10-14
Flutter Android 正式签名实战:生成密钥、配置构建与验证 APK
Flutter Android 正式签名实战:生成密钥、配置构建与验证 APK Android 正式签名不需要购买证书。开发者可以自己生成密钥,用它给 APK 签名;密钥的用途是建立稳定的应用身份。签名有效并不等于应用已经通过商店审核,也不等于应用功能已完成真机验收。 本文面向 Flutter Android 项目,介绍从开发签名切换到独立正式签名的完整流程。应用名称、目录、包名和证书信息均使用通用示例,不包含真实项目密码、私钥、签名指...
学习
flutter2026-10-14
Flutter 新版本发布实战:完整安装包与 Shorebird 热更新
一、先判断该发完整包还是补丁 | 这次改动 | 发布方式 | | | | | Dart 文案、Flutter 页面布局、业务逻辑修复 | 通常可以发布补丁 | | 纯 Dart 依赖变化 | 可以尝试补丁构建,仍需验证兼容性 | | Kotlin、Java、Swift、Objective C 或原生构建配置变化 | 发布完整包 | | 插件的原生实现变化、新增原生插件、权限变化 | 发布完整包 | | 新增或修改打包图片、字体、应用图标...
学习
docker2026-10-08
Qwik 博客全栈 Docker 本地部署实战
1\. 部署目标与服务结构 “整体容器化”是把项目运行需要的服务分别放入容器,再用 Compose 管理网络、配置、数据挂载、健康检查和启动顺序。 | Compose 服务 | 职责 | 本地访问地址 | | | | | | | Node 运行 Qwik,提供 SSR、SSG 页面和博客接口 | | | | Nginx 提供 React 构建产物,并代理管理接口 | | | | Go/Gin 管理接口 | | | | 存储博客与管理端数...
react2026-09-15
React 19.3 新特性总结
React 19.3 已发布到 npm,核心是把去年以实验 API 形式公布的 View Transitions 和 Fragment Refs 正式转正,同时新增了几个 DOM 与 RSC 相关的 API。下面按特性整理。 一、View Transitions(视图过渡动画,正式发布) 是什么 是基于浏览器原生 封装的组件。把你想要加动画的元素包起来,当元素 进入、退出、移动或尺寸变化 时,React 自动播放过渡动画。 基本用法 只...
学习

评论

请登录后发表评论

去登录
加载评论中...