【MongoDB】基本命令使用

前言

  • 对于MongoDB学习注入之前,先进行一些知识储备
  • 环境-ubuntu18.04

基本启动操作

MongoDB的服务启动

1
2
service mongodb start #启动服务
service mongodb status #确定服务是running中

查询MongoDB的端口

1
2
netstat -tulnp|grep mongo #一般27017
关闭MongoDB可以直接kill ps auxw里面杀死pid进程

打开MongoDB的客户端

1
2
find / -name mongo -print #找到客户端的位置
/usr/bin/mongo 127.0.0.1:27017 #直接进入和mysql一样登陆一样

基本命令

1
2
3
4
show dbs; #数据库
user admin; #使用x数据库
show tables; #显示当前表
db.myuser.find(); #显示myuser表中所有数据

实际情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
shijiange 0.000GB
> use shijiange
switched to db shijiange
> show tables;
myuser
> db.myuser.find()
{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }
{ "_id" : ObjectId("6109e562b3da451c7b659eb4"), "name" : "shijiange2", "age" : 30 }
{ "_id" : ObjectId("6109e567b3da451c7b659eb5"), "name" : "shijiange3", "age" : 40 }
{ "_id" : ObjectId("6109e56cb3da451c7b659eb6"), "name" : "shijiange4", "age" : 50 }
{ "_id" : ObjectId("6109e57cb3da451c7b659eb7"), "name" : "zhangsan4", "age" : 50 }
{ "_id" : ObjectId("6109e580b3da451c7b659eb8"), "name" : "zhangsan3", "age" : 50 }
{ "_id" : ObjectId("6109e58ab3da451c7b659eb9"), "name" : "zhangsan2", "age" : 40 }
{ "_id" : ObjectId("6109e58fb3da451c7b659eba"), "name" : "zhangsan1", "age" : 20 }

MongoDB集合的多种查询方式

pretty易读的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>db.myuser.find().pretty()

{
"_id" : ObjectId("6109e55db3da451c7b659eb3"),
"name" : "shijiange1",
"age" : 20
}
{
"_id" : ObjectId("6109e562b3da451c7b659eb4"),
"name" : "shijiange2",
"age" : 30
}
{
"_id" : ObjectId("6109e567b3da451c7b659eb5"),
"name" : "shijiange3",
"age" : 40
}

和mysql一样的limit查询,可以利用skip来分页查询

1
2
3
4
> db.myuser.find().limit(2)

{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }
{ "_id" : ObjectId("6109e562b3da451c7b659eb4"), "name" : "shijiange2", "age" : 30 }

MongoDB进行排序

1
2
3
4
5
6
7
8
9
10
> db.myuser.find().sort({age:1})

{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }
{ "_id" : ObjectId("6109e58fb3da451c7b659eba"), "name" : "zhangsan1", "age" : 20 }
{ "_id" : ObjectId("6109e562b3da451c7b659eb4"), "name" : "shijiange2", "age" : 30 }
{ "_id" : ObjectId("6109e567b3da451c7b659eb5"), "name" : "shijiange3", "age" : 40 }
{ "_id" : ObjectId("6109e58ab3da451c7b659eb9"), "name" : "zhangsan2", "age" : 40 }
{ "_id" : ObjectId("6109e56cb3da451c7b659eb6"), "name" : "shijiange4", "age" : 50 }
{ "_id" : ObjectId("6109e57cb3da451c7b659eb7"), "name" : "zhangsan4", "age" : 50 }
{ "_id" : ObjectId("6109e580b3da451c7b659eb8"), "name" : "zhangsan3", "age" : 50 }

根据字段进行数字比较查询

1
2
3
4
5
6
7
8
9
$gt #大于 greatest 方便记忆
$lt #小于 lowest
$gte #大于或等于 greatest equal
$lte #小于或等于 lowest equal
$ne : !=、<>
$in : in
$nin: not in
$all: all
$not: 反匹配(1.3.3及以上版本)

查询小于30的

1
2
3
4
> db.myuser.find({"age":{$lt:30}})

{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }
{ "_id" : ObjectId("6109e58fb3da451c7b659eba"), "name" : "zhangsan1", "age" : 20 }

多种查询条件之查询姓名为shijiange1的

1
2
3
> db.myuser.find({name:'shijiange1'})

{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }

or查询两条数据

1
2
3
4
> db.myuser.find({$or:[{name:'shijiange1'},{name:'shijiange2'}]})

{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }
{ "_id" : ObjectId("6109e562b3da451c7b659eb4"), "name" : "shijiange2", "age" : 30 }

and查询两个同时满足的条件

1
2
> db.myuser.find({$and:[{name:'shijiange1'},{age:20}]})
{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }

MongoDB普通正则和扩展正则查询

限制主键为name的数据shijiange[0-9]

1
2
3
4
5
6
> db.myuser.find({"name":{$regex:"shijiange[0-9]"}})

{ "_id" : ObjectId("6109e55db3da451c7b659eb3"), "name" : "shijiange1", "age" : 20 }
{ "_id" : ObjectId("6109e562b3da451c7b659eb4"), "name" : "shijiange2", "age" : 30 }
{ "_id" : ObjectId("6109e567b3da451c7b659eb5"), "name" : "shijiange3", "age" : 40 }
{ "_id" : ObjectId("6109e56cb3da451c7b659eb6"), "name" : "shijiange4", "age" : 50 }

匹配姓名中包含zhangsan的数据

1
2
3
4
5
> db.myuser.find({"name":{$regex:"(zhangsan)"}})
{ "_id" : ObjectId("6109e57cb3da451c7b659eb7"), "name" : "zhangsan4", "age" : 50 }
{ "_id" : ObjectId("6109e580b3da451c7b659eb8"), "name" : "zhangsan3", "age" : 50 }
{ "_id" : ObjectId("6109e58ab3da451c7b659eb9"), "name" : "zhangsan2", "age" : 40 }
{ "_id" : ObjectId("6109e58fb3da451c7b659eba"), "name" : "zhangsan1", "age" : 20 }

利用for循环查询100条测试数据

1
2
3
4
> for(i=1; i<=100; i++){ 
... db.myuser.insert({name:'mytest'+i, age:i})
... }
WriteResult({ "nInserted" : 1 })

image-20210804094132355

利用count查看当前表中的记录数目

1
2
> db.myuser.count()
100 #100条记录

范围查询

查询从2到10之间的数据

1
2
3
4
5
6
7
8
9
10
> db.myuser.find({"age":{$gte:2,$lte:10}})
{ "_id" : ObjectId("6109f00b92cc83f32e90ec0d"), "name" : "mytest2", "age" : 2 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec0e"), "name" : "mytest3", "age" : 3 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec0f"), "name" : "mytest4", "age" : 4 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec10"), "name" : "mytest5", "age" : 5 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec11"), "name" : "mytest6", "age" : 6 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec12"), "name" : "mytest7", "age" : 7 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec13"), "name" : "mytest8", "age" : 8 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec14"), "name" : "mytest9", "age" : 9 }
{ "_id" : ObjectId("6109f00b92cc83f32e90ec15"), "name" : "mytest10", "age" : 10 }

我的个人博客

孤桜懶契:http://gylq.github.io

本文标题:【MongoDB】基本命令使用

文章作者:孤桜懶契

发布时间:2021年08月04日 - 08:59:19

最后更新:2022年05月20日 - 11:47:45

原始链接:https://gylq.gitee.io/posts/89.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------