一.MongoDB基本概念
1.什么是MongoDB
MongoDB是一个开源的NoSQL数据库
- 使用C++编写的具有动态模式的面向文档的数据库
- 动态模式支持流畅的多态性
- 将数据存储在类似JSON的文档中(BSON)
- 使用文档(对象)更趋近于许多编程语言
2.MongoDB特点
- 高性能
- 易部署
- 易使用
- 存储数据非常方便
MongoDB是一个面向文档的基于分布式的NoSQl数据库,存储格式是类似于json格式
3.MongoDB数据模型
database
collection
index
document
field
(1)单个集合中的文档不必具有相同的字段,集合不同文档的字段的数据类型可能有所不同
(2)模式验证
创建集合或更改集合时指定JSON模式,模式验证发生在插入和更新期间,已经存在的文档不接受验证检查,直到被修改
(3)上限集合
固定大小的集合,集合填满分配的空间后,通过覆盖集合中最旧的文档来存放新文档
4.MongoDB安装
(1)配置yum源
[root@hadoop1 opt]$ vi /etc/yum.repos.d/mongodb.repo
------------------------------
[MongoDB]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1
(2)通过yum进行安装
[root@hadoop1 opt]$ yum install mongodb-org
(3)启动
[root@hadoop1 opt]$ systemctl start mongod.service
[root@hadoop1 opt]$ systemctl status mongod.service
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-03-15 09:04:57 CST; 16s ago
Docs: https://docs.mongodb.org/manual
Process: 11446 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 11444 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 11442 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 11441 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 11449 (mongod)
CGroup: /system.slice/mongod.service
└─11449 /usr/bin/mongod -f /etc/mongod.conf
Mar 15 09:04:57 hadoop1 systemd[1]: Starting MongoDB Database Server...
Mar 15 09:04:57 hadoop1 mongod[11446]: about to fork child process, waiting until server is ...ns.
Mar 15 09:04:57 hadoop1 mongod[11446]: forked process: 11449
Mar 15 09:04:57 hadoop1 systemd[1]: Started MongoDB Database Server.
Hint: Some lines were ellipsized, use -l to show in full.
(4)MongoDB工具安装
可以使用 mongodbmanager 可视化工具进行操作 MongoDB
[root@hadoop1 yum.repos.d]$ vi /etc/mongod.conf
------------需要将 bindIp 改为 0.0.0.0 -----------
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6
addresses or, alternatively, use the net.bindIpAll setting.
二.MongoDB操作
1.MongoDB基本命令
(1)启动MongoDB
[root@hadoop1 opt]$ systemctl start mongod.service
(2)查看MongoDB状态
[root@hadoop1 opt]$ systemctl status mongod.service
(3)查看MongoDB版本
[root@hadoop1 opt]$ mongod --version
db version v4.0.23
git version: 07c6611b38d2aacbdb1846b688db70b3273170fb
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
(4)启动client Shell命令
#启动client
[root@hadoop1 opt]$ mongo
MongoDB shell version v4.0.23
#显示所有数据库
>show dbs
#查看当前的数据库名字
>db
#切换数据库
>use events
#显示所有集合
>show collections
2.MongoDB的database操作
(1)创建
- use 命令后跟的数据库名,如果存在就进入此数据库,如果不存在就创建
- 使用命令use命令创建数据库后,并没有真正生成对应的数据文件,如果此时退出,此数据库将被删除,只有在此数据库中创建集合后,才会真正生成数据文件
> use events
switched to db events
> db
events
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.test1.insert({
"name":"mongodb test"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
events 0.000GB
local 0.000GB
> db.dropDatabase()
{ "dropped" : "events", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
(2)删除当前数据库
> db.dropDatabase()
(3)查看所有数据库
>show dbs
3.集合操作
(1)创建
- 显式创建
db.createCollection(“集合名称”) - 隐式创建
创建集合并同时向集合中插入数据
db.集合名称.insert({})
--显示创建
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.createCollection("mycoll")
{ "ok" : 1 }
> show tables
mycoll
> show dbs
admin 0.000GB
config 0.000GB
events 0.000GB
local 0.000GB
--隐式创建
db.test1.insert({
"name":"mongodb test"})
--创建固定大小的集合,整个集合空间大小1024000 KB
> db.createCollection("mycol2",{capped:true,size:1024000})
{ "ok" : 1 }
> show collections
mycol2
mycoll
(2)查询
> show collections
> show tables
(3)删除
--删除集合
> db.mycol2.drop()
true
> show collections
mycoll
4.文档操作
(1)插入文档
MongoDB 使用 insert() 或 save() 方法向集合中插入文档db.COLLECTION_NAME.insert(document)
直接使用集合插入文档就能创建一个集合
--单个插入
#将一条记录插入到collection表中,会自动添加_id字段,并分配_id字段的值
> db.events.insert({
"name":"demo record"}) --不推荐
> db.mycoll.insertOne({
"name":"zhangsan","age":20})
{
"acknowledged" : true,
"insertedId" : ObjectId("604ebd1d7dc9a18eb69796d5")
}
> db.mycoll.find()
{ "_id" : ObjectId("604ebd1d7dc9a18eb69796d5"), "name" : "zhangsan", "age" : 20 }
--批量插入
> db.users.insertMany([{
"name":"zhangsan"},{
"name":"lisi"},{
"name":"wangwu"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("604ebeb07dc9a18eb69796d7"),
ObjectId("604ebeb07dc9a18eb69796d8"),
ObjectId("604ebeb07dc9a18eb69796d9")
]
}
> db.users.find()
{ "_id" : ObjectId("604ebeb07dc9a18eb69796d7"), "name" : "zhangsan" }
{ "_id" : ObjectId("604ebeb07dc9a18eb69796d8"), "name" : "lisi" }
{ "_id" : ObjectId("604ebeb07dc9a18eb69796d9"), "name" : "wangwu" }
(2)查询
-
查询所有文档
-
条件查询
db.集合名称.find({条件})
属性值:db.inventory.find({ status: “D” })
过滤查询:db.inventory.find( { status: { $in: [ “A”, “D” ] } } )
--查询所有文档
> db.events.find()
> db.events.find({})
--条件查询
> db.users.find({name:"zhangsan"})
{ "_id" : ObjectId("604ebeb07dc9a18eb69796d7"), "name" : "zhangsan" }
--示例
db.inventory.insertMany([
{ item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
{ item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] },
{ item: "paper", qty: 100, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "blank", "plain" ] },
{ item: "planner", qty: 75, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank" ] },
{ item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
]);
--查找status为D查询出来
> db.inventory.find({
status:"D"})
{ "_id" : ObjectId("604ebf9c7dc9a18eb69796dc"), "item" : "paper", "qty" : 100, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "blank", "plain" ] }
{ "_id" : ObjectId("604ebf9c7dc9a18eb69796dd"), "item" : "planner", "qty" : 75, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank" ] }
--使用格式化输出
> db.inventory.find({
status:"D"}).pretty()
{
"_id" : ObjectId("604ebf9c7dc9a18eb69796dc"),
"item" : "paper",
"qty" : 100,
"status" : "D",
"size" : {
"h" : 8.5,
"w" : 11,
"uom" : "in"
},
"tags" : [
"blank",
"plain"
]
}
{
"_id" : ObjectId("604ebf9c7dc9a18eb69796dd"),
"item" : "planner",
"qty" : 75,
"status" : "D",
"size" : {
"h" : 22.85,
"w" : 30,
"uom" : "cm"
},
"tags" : [
"blank"
]
}
--查找status在A和D中的
> db.inventory.find({
status:{$in:["A","D"]}})
{ "_id" : ObjectId("604ebf9c7dc9a18eb69796da"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("604ebf9c7dc9a18eb69796db"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("604ebf9c7dc9a18eb69796dc"), "item" : "paper", "qty" : 100, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "blank", "plain" ] }
{ "_id" : ObjectId("604ebf9c7dc9a18eb69796dd"), "item" : "planner", "qty" : 75,