跳转至

Mongodb

1. 概述

SQL术语/概念 MongoDB术语/概念 解释/说明
database database 数据库
table collection 数据库表/集合
row document 数据记录行/文档
column field 数据字段/域
index index 索引
table joins 表连接,MongoDB不支持
primary key primary key 主键,MongoDB自动将_id字段设置为主键

2. 安装

  1. 从官网下载对应的发行版
  2. 解压目录
  3. 创建db数据目录和日志目录
  4. 编辑mongodb.conf配置文件(yaml格式)
  5. 配置PATH环境变量,方便使用mongo二进制文件
  6. mongodb.conf配置如下
storage:
    dbPath: /root/mongodb-5.0.9/data/db
systemLog:
    destination: file
    path: /root/mongodb-5.0.9/logs/mongod.log
    logAppend: true
net:
    bindIp: 0.0.0.0
    port: 27017
processManagement:
    fork: true       #后台运行

3. 启动

  • 后台启动mongod服务:mongod -f mongodb.conf
  • 停止mongod服务:mongod -f mongodb.conf --shutdown
  • mongo客户端登录:mongo
  • 示例:服务端启动
[root@centos7 mongodb-5.0.9]# mongod -f mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 1320
child process started successfully, parent exiting
[root@centos7 mongodb-5.0.9]# mongod -f mongodb.conf  --shutdown
killing process with pid: 1320
  • 示例:客户端登录
[root@centos7 ~]# mongo
MongoDB shell version v5.0.9
>

4. 数据库操作

4.1. 显示当前db

  • db
  • 示例:test数据库是默认db
> db
test

4.2. 显示所有db列表

title: 为什么刚创建的db没有显示出来?
因为db里面只要要有一条数据,才能显示出来,新创建的db没有任务数据
  • show dbsshow databases
  • 示例
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

4.3. 切换db

  • use 数据库名
  • 示例
> use admin
switched to db admin
> db
admin

4.4. 创建db

mongodb 使用use不仅可以切换db,还会在不存在db时创建db,再切换db
  • use 数据库名
  • 示例
> use newdb1
switched to db newdb1
> 
> db
newdb1

4.5. 删除db

  • db.dropDatabase()
  • 示例:
> db
newdb1
> db.dropDatabase()
{ "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

5. 集合(表)

5.1. 显示集合列表

  • show collections
  • 示例
> db.createCollection("coll1")
{ "ok" : 1 }
> show collections
coll1

5.2. 创建集合

- db.createCollection(name, options)

参数 类型 描述
name String 要创建的集合的名称
options Document (可选)指定有关内存大小和索引的选项

options参数是可选的,因此只需要指定集合的名称。 以下是可以使用的选项列表:

字段 类型 描述
capped Boolean (可选)如果为true,则启用封闭的集合。上限集合是固定大小的集合,它在达到其最大大小时自动覆盖其最旧的条目。 如果指定true,则还需要指定size参数
autoIndexId Boolean 3.2 之后不再支持该参数。(可选)如果为true,则在_id字段上自动创建索引。默认值为false
size 数字 (可选)指定上限集合的最大大小(以字节为单位)。 如果cappedtrue,那么还需要指定此字段的值
max 数字 (可选)指定上限集合中允许的最大文档数
在插入文档时,MongoDB首先检查上限集合capped字段的大小,然后检查max字段。
  • 示例:
> db.createCollection("coll3", {capped : true, autoIndexId : true, size : 6142800, max : 10000})
{
        "note" : "The autoIndexId option is deprecated and will be removed in a future release",
        "ok" : 1
}

5.3. 删除集合

  • 语法:db.集合名.drop()
  • 示例:
> show collections
coll1
coll2
coll3
> db.coll1.drop()
true
> show collections
coll2
coll

6. 文档(表行)

- 文档的数据结构和 JSON 基本一样。
- 所有存储在集合中的数据都是 BSON 格式。
- BSON 是一种类似 JSON 的二进制形式的存储格式,是 Binary JSON 的简称。

6.1. 插入文档

  • 语法:
    • db.集合名.insert()
    • db.集合名.save()
    • db.集合名.insertOne():(3.2版本后)插入一条
    • db.集合名.insertMany():(3.2版本后)插入多个
  • 示例
> db.test.insertOne({name:"boo",age:20})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("62b89648de6d7f3e9b6b2c46")
}
> db.test.insertMany([{name:"tom",age:19},{name:"tim",age:20}])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("62b896cfde6d7f3e9b6b2c47"),
                ObjectId("62b896cfde6d7f3e9b6b2c48")
        ]
}
> db.test.save({name:"chery",age:18})
WriteResult({ "nInserted" : 1 })
> db.test.insert({name:"jim",age:19})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("62b89648de6d7f3e9b6b2c46"), "name" : "boo", "age" : 20 }
{ "_id" : ObjectId("62b896cfde6d7f3e9b6b2c47"), "name" : "tom", "age" : 19 }
{ "_id" : ObjectId("62b896cfde6d7f3e9b6b2c48"), "name" : "tim", "age" : 20 }
{ "_id" : ObjectId("62b89847de6d7f3e9b6b2c49"), "name" : "chery", "age" : 18 }
{ "_id" : ObjectId("62b89875de6d7f3e9b6b2c4a"), "name" : "jim", "age" : 19 }

6.2. 更新文档

  • 语法:db.collection.update(<query>,<update>,{upsert: <boolean>,multi: <boolean>,writeConcern: <document>})
    • query : update的查询条件,类似sql update查询内where后面的。
    • update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
    • upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
    • multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
    • writeConcern :可选,抛出异常的级别。
  • db.collection.updateOne(<filter>, <update>, <options>):更新匹配的一个文档
  • db.collection.updateMany(<filter>, <update>, <options>):更新匹配的多个文档
  • db.collection.replaceOne(<filter>, <update>, <options>):替换匹配的一个文档
title: update、updateOne、updateMany间联系?
- 当multi选项是false时,update等价于updateOne
- 当multi选项是ture时,update等价于updateMany
  • 示例:update
> db.test.insert({id:001,name:"boo",age:19})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 19 }
> db.test.update({name:"boo"},{$set:{age:20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 20 }
  • 示例:update multi=true
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 21 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "id" : 3, "name" : "Alice", "age" : 21 }
> db.test.update({age:21},{$set:{age:22}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 22 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "id" : 3, "name" : "Alice", "age" : 22 }
  • 示例:update 用来替换
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 21 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "age" : 21 }
> db.test.update({age:21},{age:22})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "age" : 22 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "age" : 21 }
  • 示例:updateOne
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 22 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "id" : 3, "name" : "Alice", "age" : 22 }
> db.test.updateOne({age:22},{$set:{age:21}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 21 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "id" : 3, "name" : "Alice", "age" : 22 }
  • 示例:updateMany
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 20 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "id" : 3, "name" : "Alice", "age" : 20 }
> db.test.updateMany({age:20},{$set:{age:21}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 21 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "id" : 3, "name" : "Alice", "age" : 21 }
  • 示例:replaceOne
> db.test.replaceOne({age:22},{age:21})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "id" : 1, "name" : "boo", "age" : 21 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "age" : 21 }

6.3. 删除文档

  • 语法:db.collection.remove(<query>,{justOne: <boolean>,writeConcern: <document>})
    • query :(可选)删除的文档的条件。
    • justOne : (可选)如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。
    • writeConcern :(可选)抛出异常的级别。
  • 示例
> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "age" : 22 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "age" : 21 }
{ "_id" : ObjectId("62b8e12c369975e7c1bb96c8"), "id" : 1, "name" : "boo", "age" : 19 }
{ "_id" : ObjectId("62b8e13c369975e7c1bb96c9"), "id" : 1, "name" : "Alice", "age" : 20 }
> db.test.remove({age:21})
WriteResult({ "nRemoved" : 1 })
> db.test.remove({age:222})
WriteResult({ "nRemoved" : 0 })
> db.test.remove({age:22})
WriteResult({ "nRemoved" : 1 })
> db.test.find()
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8e12c369975e7c1bb96c8"), "id" : 1, "name" : "boo", "age" : 19 }
{ "_id" : ObjectId("62b8e13c369975e7c1bb96c9"), "id" : 1, "name" : "Alice", "age" : 20 }

6.4. 查询文档

> db.test.find()
{ "_id" : ObjectId("62b8d9e9369975e7c1bb96c5"), "age" : 22 }
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8db1e369975e7c1bb96c7"), "age" : 21 }
> db.test.find({age:21})
  • 示例:值比较查询
> db.test.find()
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8e12c369975e7c1bb96c8"), "id" : 1, "name" : "boo", "age" : 19 }
{ "_id" : ObjectId("62b8e13c369975e7c1bb96c9"), "id" : 1, "name" : "Alice", "age" : 20 }
> db.test.find({age:{$lte:19}})
{ "_id" : ObjectId("62b8db0e369975e7c1bb96c6"), "id" : 2, "name" : "Jim", "age" : 19 }
{ "_id" : ObjectId("62b8e12c369975e7c1bb96c8"), "id" : 1, "name" : "boo", "age" : 19 }

7. 操作符

7.1. 查询

  • https://www.mongodb.com/docs/v4.0/reference/operator/query/

8. 聚合操作