1、MongoDB使用教程

2、mongodb介绍

2.1 关系型数据库和非关系型数据库区别

databasedatabase数据库
tablecollection数据库表/集合
rowdocument数据记录行/文档
columnfield数据字段/域
indexindex索引
table joins 表连接,mongodb不支持
primary keyprimary key主键,mongdb自动将_id字段设置为主键

下表说明各自的优缺点以及特性

关系型数据库1、关系型数据库,是指采用了关系模型来组织数据的数据库;2、关系型数据库的最大特点就是事务的一致性;3、简单来说,关系模型指的就是二维表格模型,而一个关系型数据库就是由二维表及其之间的联系所组成的一个数据组织。1、容易理解:二维表结构是非常贴近逻辑世界一个概念,关系模型相对网状、层次等其他模型来说更容易理解;2、使用方便:通用的SQL语言使得操作关系型数据库非常方便;3、易于维护:丰富的完整性(实体完整性、参照完整性和用户定义的完整性)大大减低了数据冗余和数据不一致的概率;4、支持SQL,可用于复杂的查询。1、为了维护一致性所付出的巨大代价就是其读写性能比较差;2、固定的表结构;3、高并发读写需求;4、海量数据的高效率读写;
非关系型数据库1、使用键值对存储数据;2、分布式;3、一般不支持ACID特性;4、非关系型数据库严格上不是一种数据库,应该是一种数据结构化存储方法的集合。1、无需经过sql层的解析,读写性能很高;2、基于键值对,数据没有耦合性,容易扩展;3、存储数据的格式:nosql的存储格式是key,value形式、文档形式、图片形式等等,而关系型数据库则只支持基础类型。1、不提供sql支持,学习和使用成本较高;2、无事务处理,附加功能和报表等支持也不好;

3、操作mongodb

3.1 安装mongodb

3.2 打开数据库连接池

mongod --dbpath d:\data\db

// win10如果报错
./mongod --dbpath d:\data\db

如果还不可用,就用管理员身份去运行

最后还不可用,换电脑或者换系统

如果输出 waiting for connections on port 27017 表明连接池打开成功

3.3 打开命令行的数据库客户端

打开压缩的mongodb文件夹,进入bin目录

shift + 右键 选择打开 命令行窗口

./mongo          //链接数据库

3.4 数据库常用命令

admin       0.000GB
local       0.000GB
switched to db sh1908
sh1908
{
  "db" : "sh1908",
  "collections" : 0,
  "views" : 0,
  "objects" : 0,
  "avgObjSize" : 0,
  "dataSize" : 0,
  "storageSize" : 0,
  "numExtents" : 0,
  "indexes" : 0,
  "indexSize" : 0,
  "fileSize" : 0,
  "ok" : 1
}
3.4.2
connection to 127.0.0.1:27017
{ "ok" : 1 }

4、collection 聚集集合操作

4.1、创建集合

{ "ok" : 1 }
capped布尔(可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。当该值为 true 时,必须指定 size 参数。
autoIndexId布尔(可选)如为 true,自动在 _id 字段创建索引。默认为 false。
size数值(可选)为固定集合指定一个最大值(以字节计)。如果 capped 为 true,也需要指定该字段。
max数值(可选)指定固定集合中包含文档的最大数量。
{ "ok" : 1 }

4.2 得到指定名称的聚集集合

sh1908.user

4.3 得到当前DB的所有的聚集集合

[ "course", "users" ]

4.4 显示当前db所有集合的状态

5、document文档操作

增删改查

5.1 插入操作 ----- 增

sh1908.course
WriteResult({ "nInserted" : 1 })

插入单条数据

插入多条数据

了解

{
  "acknowledged" : true,
  "insertedId" : ObjectId("5da5690eee7de50b8cbc6ea5")
}
{
  "acknowledged" : true,
  "insertedIds" : [
      ObjectId("5da56a10ee7de50b8cbc6ea6"),
      ObjectId("5da56a10ee7de50b8cbc6ea7")
  ]
}

5.2 简单查询插入的数据 ---- 看一下数据是什么

{ "_id" : ObjectId("5da5690eee7de50b8cbc6ea5"), "username" : "小明", "password" : "123456", "sex" : 1, "age" : 18, "lesson" : 3, "city" : "山西" }
{ "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"), "username" : "小红", "password" : "123456", "sex" : 1, "age" : 25, "lesson" : 3, "city" : "安徽" }
{ "_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"), "username" : "小兰", "password" : "123456", "sex" : 1, "age" : 40, "lesson" : 3, "city" : "安徽" }
{
  "_id" : ObjectId("5da5690eee7de50b8cbc6ea5"),
  "username" : "小明",
  "password" : "123456",
  "sex" : 1,
  "age" : 18,
  "lesson" : 3,
  "city" : "山西"
}
{
  "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
  "username" : "小红",
  "password" : "123456",
  "sex" : 1,
  "age" : 25,
  "lesson" : 3,
  "city" : "安徽"
}
{
  "_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"),
  "username" : "小兰",
  "password" : "123456",
  "sex" : 1,
  "age" : 40,
  "lesson" : 3,
  "city" : "安徽"
}

5.3 删除数据

{ "acknowledged" : true, "deletedCount" : 1 }
{
      "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
      "username" : "小红",
      "password" : "123456",
      "sex" : 1,
      "age" : 25,
      "lesson" : 3,
      "city" : "安徽"
}
{
      "_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"),
      "username" : "小兰",
      "password" : "123456",
      "sex" : 1,
      "age" : 40,
      "lesson" : 3,
      "city" : "安徽"
}
{ "acknowledged" : true, "deletedCount" : 1 }
{
      "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
      "username" : "小红",
      "password" : "123456",
      "sex" : 1,
      "age" : 25,
      "lesson" : 3,
      "city" : "安徽"
}
{ "acknowledged" : true, "deletedCount" : 1 }
// 没有返回

小结

  • db.col.deleteOne({key: value}) 删除单条数据

  • db.col.deleteMany({key: value}) 删除多条数据

  • db.col.deleteMany({}) 删除所有的数据

5.4 修改数据

依据5.1步骤插入数据

{                        西'})
      "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
      "username" : "小明",
      "password" : "123456",
      "sex" : 1,
      "age" : 18,
      "lesson" : 3,
      "city" : "山西"
}
{
      "_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
      "username" : "小红",
      "password" : "123456",
      "sex" : 1,
      "age" : 25,
      "lesson" : 3,
      "city" : "安徽"
}
{
      "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
      "username" : "小兰",
      "password" : "123456",
      "sex" : 1,
      "age" : 40,
      "lesson" : 3,
      "city" : "安徽"
}
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

> db.users.updateMany({}, { $set: { company: '阿里'}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
 

> db.users.find().pretty()

{ "id" : ObjectId("5da56dbeee7de50b8cbc6ea8"), "username" : "小明", "password" : "123456", "sex" : 1, "age" : 20, "lesson" : 3, "city" : "山西", "company" : "阿里" }

{ "id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"), "username" : "小红", "password" : "123456", "sex" : 1, "age" : 25, "lesson" : 3, "city" : "安徽", "company" : "阿里" }

 { "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"), "username" : "小兰", "password" : "123456", "sex" : 1, "age" : 40, "lesson" : 3, "city" : "安徽", "company" : "阿里" }
> db.users.updateMany({}, { $inc: {age: 1}}) // 所有的年龄 +1

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.users.find().pretty()

{ "id" : ObjectId("5da56dbeee7de50b8cbc6ea8"), "username" : "小明", "password" : "123456", "sex" : 1, "age" : 21, "lesson" : 3, "city" : "山西", "company" : "阿里" }

{ "id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"), "username" : "小红", "password" : "123456", "sex" : 1, "age" : 26, "lesson" : 3, "city" : "安徽", "company" : "阿里" }

{ "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"), "username" : "小兰", "password" : "123456", "sex" : 1, "age" : 41, "lesson" : 3, "city" : "安徽", "company" : "阿里" }

> db.users.updateMany({}, { $inc: {age: -3}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.users.find().pretty()

{ "id" : ObjectId("5da56dbeee7de50b8cbc6ea8"), "username" : "小明", "password" : "123456", "sex" : 1, "age" : 18, "lesson" : 3, "city" : "山西", "company" : "阿里" }

 { "id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"), "username" : "小红", "password" : "123456", "sex" : 1, "age" : 23, "lesson" : 3, "city" : "安徽", "company" : "阿里" }

{ "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"), "username" : "小兰", "password" : "123456", "sex" : 1, "age" : 38, "lesson" : 3, "city" : "安徽", "company" : "阿里" }

**小结**

* db.col.updateOne({key:value}, {$set: {key1: value1}})
* db.col.updateOne({key:value}, {$inc: {key1: 1}})
* db.col.updateMany({key:value}, {$set: {key1: value1}})
* db.col.updateMany({key:value}, {$inc: {key1: 1}})
* db.col.updateMany({}, {$set: {key1: value1}})
* db.col.updateMany({}, {$inc: {key1: 1}})

## 5.5 查询数据

> db.users.find().pretty()

> db.users.find({},{}).pretty() // 同上,第一个{}代表查询的条件,第二个代表显示的字段

> db.users.find({username:'小明'}).pretty() // 查询用户名为小明的记录

{ "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"), "username" : "小明", "password" : "123456", "sex" : 1, "age" : 18, "lesson" : 3, "city" : "山西", "company" : "阿里" }

> db.users.find({username:'小明'}, {username: 1, age: 1}).pretty()

{ "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"), "username" : "小明", "age" : 18 }


> db.users.find({username:'小明'}, {username: 1, age: 1, _id: 0}).pretty()

{ "username" : "小明", "age" : 18 }

> db.users.find({}, {username: 1, age: 1, _id: 0}).pretty()

{ "username" : "小明", "age" : 18 } { "username" : "小红", "age" : 23 } { "username" : "小兰", "age" : 38 }

> db.users.find({ age: { $gte: 30} }, {_id:0, username: 1}).pretty() // 查询大于等于30岁的数据  大于用 $gt

{ "username" : "小兰" }

> db.users.find({ age: { $gte: 18, $lte: 30} }, {_id:0, username: 1}).pretty()

{ "username" : "小明" } { "username" : "小红" }


> db.users.find({}, {_id:0, username: 1, age: 1}).sort({age: 1}).pretty() // 年龄的升序

{ "username" : "小明", "age" : 18 } { "username" : "小红", "age" : 23 } { "username" : "小兰", "age" : 38 }


> db.users.find({}, {_id:0, username: 1, age: 1}).sort({age: -1}).pretty() // 年龄的降序

{ "username" : "小兰", "age" : 38 } { "username" : "小红", "age" : 23 } { "username" : "小明", "age" : 18 }


> db.users.find({username: /明/}, {}).pretty() // 查询名字中带明的数据---模糊查询

{ "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"), "username" : "小明", "password" : "123456", "sex" : 1, "age" : 18, "lesson" : 3, "city" : "山西", "company" : "阿里" }

> db.users.find({username: /^明/}, {}).pretty() // 查询名字中带明且开头的数据---模糊查询

{ "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"), "username" : "小明", "password" : "123456", "sex" : 1, "age" : 18, "lesson" : 3, "city" : "山西", "company" : "阿里" }


> db.users.distinct('city') // 查询所有数据的city字段组成数组,并且去重

[ "山西", "安徽" ]


> db.users.distinct('age')

[ 18, 23, 38 ]


> db.users.find().count() // 查询的条数

3


* 想要查看其他的查询方法 db.col.find().help()

 


**小结**
* db.col.find()
* db.col.find().pretty()
* db.col.find({key: value})
* db.col.find({}, {_id:0, key1: 1})
* db.col.find({price: { $gte: 100, $lte: 200}})
* db.col.find().count()
* db.col.distinct('key')
* db.col.find().toArray() // 转换成数组
* db.col.find().limit(num) // 只能查询num条数据
* db.col.find().skip(n) // 从第n条数据开始查询,下标从0开始
* db.col.find($or: [{age:18, lesson:2}]) // 查询年龄为18 或者 二阶段的数据
01-20 18:45