当前位置:网站首页>MongoDB - query
MongoDB - query
2022-07-30 07:43:00 【HDLaZy】
1:查询
Query all documents in the collection:
db.集合名.find()
对查询结果进行去重:
db.集合名.distinct('指定Key')
查询指定字段:
db.集合名.find(
{},#查询条件,类似于where
{
'filed1':true,'filed2':false}#Set the fields of return,类似于select,true为返回,false为不返回,可以使用0代表false,1代表true
)
2:运算符
| 运算符 | 对应 |
|---|---|
| > | $gt |
| < | $lt |
| >= | $gte |
| <= | $lte |
| != | $ne |
#查询年纪在16-19之间,Class for the students of class 2
db.student.find(
{
'age':{
'$gte':16,'$lte':19},'class_name':{
'$ne':'二班'}},
{
'_id',0,'name':1,'class_name':1}
)
and和or:
#Query class for class 2,And age is more than17的学生
db.student.find(
{
'class_name':'二班','age':{
'$gt':17}}
)
#Query class for class 2 or class 3 students
db.student.find(
{
'$or':[{
'class_name':'二班'},{
'class_name':'三班'}]}
)
3:count/limit
count:Returns the number conforms to the result of
#Check the number of class 3 boys
db.studnet.fine(
{
'class_name':'三班','sex':'男'}
).count()
limit:Set the query results back number
#Query class three age greater than22的三个人
db.student.find(
{
'class_name':'三班','age':{
'$get':22}}
).limit(3)
spik:Set from subscript for several began to return to the,起始下标为0,默认为0
#Query class 3 people,From the third document began to return to the4条
#返回的下标2,3,4,5
#The actual document number3,4,5,6
db.student.find(
{
'class_name':'三班'}
).limit(4).skip(2)
4:排序查询结果
sort:The result of the query according to a certainkey进行排序
参数1表示升序
参数-1表示降序
#Returns the class three oldest people age
db.studnet.find(
{
'class_name':'三班'},
{
'age':1,'_id':0}
).sort({
'age':-1}).limit(1)
5:分组和聚合函数
group:max,min,avg,sum
参数_id:Said the grouping field
#Query classes the oldest man
db.student.aggregate(
{
'$group':{
'_id':'class_name','最大年纪':{
'$max':'$age'}}}
)
6:模糊查询
- //包含
- /^/开头
- /$/结尾
#Name contains li
db.student.find(
{
'name':/李/}
)
#Starting with the king
db.student.find(
{
'name':/^王/}
)
#In the sea at the end of the
db.student.find(
{
'name':/海$/}
)
边栏推荐
猜你喜欢
随机推荐
Mastering JESD204B (2) – Debugging of AD6676
关于memcache内核,全网最通俗的讲解
测试开发工程师成长日记015 - 最强20道测试面试题
The concept and testing method of black box testing
替换xxx.jar的class文件命令
MongoDB-CUD没有R
THREEJS导入外部OBJ+MTL后内存优化
测试开发工程师成长日记002 - 从0开始做接口自动化
Desthiobiotin-PEG4-Acid|脱硫生物素-PEG4-酸| 供应商和制造商
测试开发工程师成长日记001 - 敏捷测试、CI/CD/CT、DecOps的一些介绍
藏不住了,我要揭露云原生的那些不好
不依赖框架的文件下载
MongoDB-查询
mpich安装
作为测试leader,考察求职者的几个方面
Network Protocol 01 - Basic Concepts
JSP自定义标签
网络协议03 - 路由和NAT
如何将matlab数据导入modelsim仿真
04-加壳和脱壳









