当前位置:网站首页>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})
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!
边栏推荐
- SQOOP 1.4.6 INSTALL
- Flink calculates topn hot list in real time
- flume 190 INSTALL
- MPLS experiment
- Flutter环境配置保姆级教程,让doctor一绿到底
- Leetcode+ 76 - 80 storm search topic
- What is the significance of the college entrance examination
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
- 12. Process synchronization and semaphore
猜你喜欢
Pytest-- test report allure configuration
Operator-1初识Operator
Determine whether there are duplicate elements in the array
集成学习概览
MySQL数据库远程访问权限设置
Easyexcel, a concise, fast and memory saving excel processing tool
Stm32 et développement de moteurs (système supérieur)
JS reduce accumulator
13. Semaphore critical zone protection
VSCode工具使用
随机推荐
从MediaRecord录像中读取H264参数
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
Pywin32打开指定窗口
Stm32 et développement de moteurs (système supérieur)
2022-06-17
Flink submitter
UVM - usage of common TLM port
13. Semaphore critical zone protection
2. Hacking lab script off [detailed writeup]
Allure -- common configuration items
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
14. Code implementation of semaphore
[SUCTF2018]followme
Operator-1初识Operator
12. Process synchronization and semaphore
Is this code PHP MySQL redundant?
MPLS experiment
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
PCL 投影点云
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘