当前位置:网站首页>MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
2022-07-02 07:12:00 【JAVA·D·WangJing】
一、添加测试数据
### 插入测试数据
db.wangjing_test.insert({"name":"wangjing","sex":"男","age":27,"height":175,"weight":100});
db.wangjing_test.insert({"name":"wangjing1","sex":"男","age":37,"height":185,"weight":110});
db.wangjing_test.insert({"name":"wangjing2","sex":"男","age":47,"height":195,"weight":120});
db.wangjing_test.insert({"name":"wangjing3","sex":"男","age":57,"height":205,"weight":130});
二、条件操作符
#### 条件操作符用于比较两个表达式并从mongoDB集合中获取数据。
### MongoDB中条件操作符有:
# (>) 大于 - $gt
# (<) 小于 - $lt
# (>=) 大于等于 - $gte
# (<= ) 小于等于 - $lte
### (>=)大于等于操作符 - $gte
# 查询 age 大于等于 37 岁
db.wangjing_test.find({age : {$gte : 37}})
### (>)大于操作符 - $gt
# 查询年龄大于 37 岁
db.wangjing_test.find({age : {$gt : 37}})
### MongoDB (<=) 小于等于操作符 - $lte
# 查询年龄小于等于 37 岁
db.wangjing_test.find({age : {$lte : 37}})
### (<) 小于操作符 - $lt
# 查询年龄小于 37 岁
db.wangjing_test.find({age : {$lt : 37}})
### MongoDB 使用 (<) 和 (>) 查询 - $lt 和 $gt
# 查询年龄小于50岁,大于30岁
db.wangjing_test.find({age : {$lt : 50, $gt : 30}})
三、$type 操作符
#### $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。
### 中可以使用的类型:
# 类型 数字 备注
# Double 1
# String 2
# Object 3
# Array 4
# Binary data 5
# Undefined 6 已废弃。
# Object id 7
# Boolean 8
# Date 9
# Null 10
# Regular Expression 11
# JavaScript 13
# Symbol 14
# JavaScript (with scope) 15
# 32-bit integer 16
# Timestamp 17
# 64-bit integer 18
# Min key 255 Query with -1.
# Max key 127
# 查询 name 为字符串的数据
db.wangjing_test.find({"name" : {$type : 2}})
四、limit()方法
#### limit 方法来指定查询的数量,limit()方法接受一个数字参数。
# 查询两条数据
db.wangjing_test.find().limit(2)
五、skip() 方法
#### skip()方法来跳过指定数量的数据,skip方法同样接受一个数字参数作为跳过的记录条数。
# 查询第二条数据
db.wangjing_test.find().limit(1).skip(1)
六、sort() 方法
#### sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。
# 根据年龄降叙查询
db.wangjing_test.find().sort({age:-1})
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!
边栏推荐
- Webui automated learning
- Pywin32 opens the specified window
- UVM——Callback
- Set the playback speed during the playback of UOB equipment
- [visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened
- Excuse me, is it cost-effective to insure love life patron saint 2.0 increased lifelong life insurance? What are the advantages of this product?
- [jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found
- SQOOP 1.4.6 INSTALL
- MYSQL关键字
- Easyexcel, a concise, fast and memory saving excel processing tool
猜你喜欢
随机推荐
MongoDB-快速上手MongoDB命令行的一些简单操作
Kustomize user manual
[jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found
2. Hacking lab script off [detailed writeup]
Sum the two numbers to find the target value
Start class, data analysis, high salary training plan, elite class
UWA report uses tips. Did you get it? (the fourth bullet)
Disassembling Meitu SaaS: driving the plane to change the engine
lunix重新分配root 和 home 空间内存
PCL之K-d树与八叉树
shell编程01_Shell基础
js setTimeout()与面试题
stm32和電機開發(上比特系統)
从MediaRecord录像中读取H264参数
Solutions to a series of problems in sqoop job creation
[unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
Kustomize使用手册
Database dictionary Navicat automatic generation version
UVM——Callback
axis设备的rtsp setup头中的url不能带参

![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)

![[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces](/img/6f/e5a30dae824ccb22d1157818be58be.png)





