MongoDB增删改查基本操作
文章发布较早,内容可能过时,阅读注意甄别。
本文介绍 MongoDB 的日常增删改查命令操作。
Mongodb 中关键字种类:
- db(数据库实例级别)
- db 本身
- db.connection 数据库下的集合信息
- db.collection.xxx(
- rs(复制集级别)
- sh(分片级别)
# 1,查询操作
注意,以下所有操作,都是基于用户认证关闭的配置来进行的,配置如下所示:
[root@mongodb bin]$cat mongodb.conf
#数据存储目录
dbpath=/usr/local/mongodb/data/db
#日志文件目录
logpath=/usr/local/mongodb/logs/mongodb.log
#后台运行
fork=true
auth=false
bind_ip=0.0.0.0
[root@mongodb bin]$systemctl restart mongod
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
在客户端指定数据库进行连接:(默认连接本机 test 数据库)
[root@mongodb bin]$mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e083a0dd-61a2-4e02-8e48-20ddf70dc701") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten]
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten]
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten]
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-07-03T13:58:46.421+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> db
test
> db.disableFreeMonitoring() #首先关闭上边最后一句提示的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
解决一下进入之后的烦人提示:
[root@mongodb bin]$ echo nerver > /sys/kernel/mm/transparent_hugepage/defrag
[root@mongodb bin]$ echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@mongodb bin]$ systemctl restart mongod
1
2
3
2
3
然后重启一下应用,为了避免服务器重启失效,写入开启自动执行:
cat >> /etc/rc.local <<'EOF'
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
再次进入:
[root@localhost bin]$mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e9a0ecdb-e17a-4b0a-b0b6-00cbddabe3f7") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-04T00:11:48.841+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-07-04T00:11:48.841+0800 I CONTROL [initandlisten]
>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 1,查看当前数据库版本
> db.version()
4.0.10
1
2
2
# 2,切换数据库
> use admin
switched to db admin
1
2
2
# 3,显示当前数据库
> db
admin
> db.getName()
admin
1
2
3
4
2
3
4
# 4,查询所有数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 5,查看 admin 数据库当前状态
> use admin
switched to db admin
> db.stats()
{
"db" : "admin",
"collections" : 1,
"views" : 0,
"objects" : 1,
"avgObjSize" : 59,
"dataSize" : 59,
"storageSize" : 16384,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 16384,
"fsUsedSize" : 13630574592,
"fsTotalSize" : 210031038464,
"ok" : 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 6,查看当前数据库的连接机器地址
> db.getMongo()
connection to 127.0.0.1:27017
1
2
2
# 2,数据管理
# 1,创建数据库
> use eryajf
switched to db eryajf
1
2
2
说明:
当 use 的时候,系统就会自动创建一个数据库。
如果 use 之后没有创建任何集合。系统就会删除这个数据库。
# 2,删除数据库
> db
eryajf
> show dbs
admin 0.000GB
config 0.000GB
eryajf 0.000GB
local 0.000GB
> db.dropDatabase()
{ "dropped" : "eryajf", "ok" : 1 }
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
说明:
删除数据库:如果没有选择任何数据库,会删除默认的 test 数据库
# 3,创建集合
- 方法一
> use eryajf
switched to db eryajf
> db.createCollection('a')
{ "ok" : 1 }
> db.createCollection('b')
{ "ok" : 1 }
1
2
3
4
5
6
2
3
4
5
6
- 查看当前数据下的所有集合
> show collections
a
b
> db.getCollectionNames()
[ "a", "b" ]
1
2
3
4
5
2
3
4
5
方法二
当插入一个文档的时候,一个集合就会自动创建。
> db.c.insert({name:'eryajf'})
WriteResult({ "nInserted" : 1 })
> db.c.insert({url:'http://eryajf.net'})
WriteResult({ "nInserted" : 1 })
1
2
3
4
2
3
4
# 4,查看创建的合集
> db.getCollectionNames()
[ "a", "b", "c" ]
1
2
2
- 查看合集里的内容
> db.c.find()
{ "_id" : ObjectId("5d1c48e5e6a475def02382cc"), "name" : "eryajf" }
{ "_id" : ObjectId("5d1c48f9e6a475def02382cd"), "url" : "http://eryajf.net" }
1
2
3
2
3
# 5,重命名集合
> db.c.renameCollection("eryajf")
{ "ok" : 1 }
> db.getCollectionNames()
[ "a", "b", "eryajf" ]
1
2
3
4
2
3
4
# 6,删除合集
> db.a.drop()
true
> db.getCollectionNames()
[ "b", "eryajf" ]
1
2
3
4
2
3
4
# 7,插入 1w 行数据
> for(i=0;i<10000;i++){ db.log.insert({"uid":i,"name":"mongodb","age":6,"date":new Date()}); }
WriteResult({ "nInserted" : 1 })
1
2
2
查询集合中的查询所有记录
> db.log.find()
1
注:默认每页显示 20 条记录,当显示不下的的情况下,可以用 it 迭代命令查询下一页数据。
> DBQuery.shellBatchSize=50; # 每页显示50条记录
> db.log.findOne() # 查看第1条记录
{
"_id" : ObjectId("5d1c4963e6a475def02382ce"),
"uid" : 0,
"name" : "mongodb",
"age" : 6,
"date" : ISODate("2019-07-03T06:21:23.125Z")
}
> db.log.count() # 查询总的记录数
10000
> db.log.find({uid:1000}) # 查询UUID为1000的数据
{ "_id" : ObjectId("5d1c4963e6a475def02386b6"), "uid" : 1000, "name" : "mongodb", "age" : 6, "date" : ISODate("2019-07-03T06:21:23.529Z") }
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
删除集合中的记录数
> db.log.distinct("name") # 查询去掉当前集合中某列的重复数据
[ "mongodb" ]
> db.log.remove({}) # 删除集合中所有记录
WriteResult({ "nRemoved" : 10000 })
> db.log.distinct("name")
[ ]
1
2
3
4
5
6
2
3
4
5
6
查看集合存储信息
> db.log.stats() # 查看数据状态
> db.log.dataSize() # 集合中数据的原始大小
> db.log.totalIndexSize() # 集合中索引数据的原始大小
> db.log.totalSize() # 集合中索引+数据压缩存储之后的大小
> db.log.storageSize() # 集合中数据压缩存储的大小
1
2
3
4
5
2
3
4
5
参考:http://t.cn/AiOcP9Cy
上次更新: 2024/12/05, 21:39:23