当前位置:网站首页>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})
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!
边栏推荐
猜你喜欢

Retrofit's callback hell is really vulnerable in kotlin synergy mode!

stm32和电机开发(上位系统)

从MediaRecord录像中读取H264参数

Considerations for Apache deploying static web page projects

MongoDB-快速上手MongoDB命令行的一些简单操作

【TS】1368- 秒懂 TypeScript 泛型工具类型!

sqoop创建job出现的一系列问题解决方法

Solutions to a series of problems in sqoop job creation

Mongodb quickly get started with some simple operations of mongodb command line

KS009基于SSH实现宠物管理系统
随机推荐
[unity3d] production progress bar - make image have the functions of filled and sliced at the same time
Is this code PHP MySQL redundant?
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
Introduction to MySQL 8 DBA foundation tutorial
Flutter环境配置保姆级教程,让doctor一绿到底
js promise.all
Basic usage of mock server
SUS系统可用性量表
Considerations for Apache deploying static web page projects
Ks009 implement pet management system based on SSH
SQOOP 1.4.6 INSTALL
面对不确定性,供应链的作用
SPSS做Shapiro-Wilk正态分析
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Network communication learning
Operator-1 first acquaintance with operator
MPLS experiment
Windows环境MySQL8忘记密码文件解决方案
How to get the password of cpolar?
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer