MongoDB 是一个基于分布式文件存储的数据库, 是一个介于关系数据库和非关系数据库之间的产品,实质上是非关系数据库。
本地安装MongoDB
1.下载对应的版本,注意windows兼容的版本,这个一定区分清楚,
下载地址:Download MongoDB Community Server | MongoDB
2.新建data文件夹和文件夹

3.安装
双击:mongodb-win32-x86_64-2008plus-ssl-3.6.23-signed.msi文件开始安装
4.cmd命令框运行:mongod -dbpath 安装地址\data
5.访问地址:http://localhost:27017(如果不报错,本地环境就安装好了)


MongoDB操作以及关键字:
基本的概念是文档、集合、数据库,这个概念有点抽象,那么我们对比sql来深入了解一下,没有sql经验的同学,先学习下Mysql
| SQL术语/概念 | MongoDB术语/概念 | 解释/说明 |
| database | database | 数据库 |
| Table(表格) | Collection(集合) | 数据库表/集合 |
| Row(行) | Document(文档) | 数据记录行/文档 |
| Column(列) | Field(字段) | 数据字段/域 |
| index | index | 索引 |
| table joins(联合查询) | (嵌入文档) | 表连接,MongoDB不支持 |
| primary key | primary key | 主键,MongoDB自动将_id字段设置为主键 |
MongoDB数据类型:
| 数据类型 | 描述 |
| String | 字符串。存储数据常用的数据类型。在 MongoDB 中,UTF-8 编码的字符串才是合法的。 |
| Integer | 整型数值。用于存储数值。根据你所采用的服务器,可分为 32 位或 64 位。 |
| Boolean | 布尔值。用于存储布尔值(真/假)。 |
| Double | 双精度浮点值。用于存储浮点值。 |
| Min/Max keys | 将一个值与 BSON(二进制的 JSON)元素的最低值和最高值相对比。 |
| Array | 用于将数组或列表或多个值存储为一个键。 |
| Timestamp | 时间戳。记录文档修改或添加的具体时间。 |
| Object | 用于内嵌文档。 |
| Null | 用于创建空值。 |
| Symbol | 符号。该数据类型基本上等同于字符串类型,但不同的是,它一般用于采用特殊符号类型的语言。 |
| Date | 日期时间。用 UNIX 时间格式来存储当前日期或时间。你可以指定自己的日期时间:创建 Date 对象,传入年月日信息。 |
| Object ID | 对象 ID。用于创建文档的 ID。 |
| Binary Data | 二进制数据。用于存储二进制数据。 |
| Code | 代码类型。用于在文档中存储 JavaScript 代码。 |
| Regular expression | 正则表达式类型。用于存储正则表达式。 |
MongoDB相关命令:
1.查看数据库:
Show dbs

2.插入数据:
use database【这一步相当于创建】
db.database.insertOne(json格式数据)【这一步插入数据】

3.删除数据库:
Db.dropdatabase

4.创建集合
Use database
db.createCollection(name, options)
Eg:db.createCollection("message",{size:80})

| 段 | 类型 | 描述 |
| capped | 布尔 | (可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。 |
| autoIndexId | 布尔 | 3.2 之后不再支持该参数。(可选)如为 true,自动在 _id 字段创建索引。默认为 false。 |
| size | 数值 | (可选)为固定集合指定一个最大值,即字节数。 |
| max | 数值 | (可选)指定固定集合中包含文档的最大数量。 |
5.查看集合
show collections 或 show tables

6.删除集合
db.collection.drop()

7.插入文档(集合中插入数据:document结构为json):
db.collect.insert(document)

8.修改文档(document结构为json):
db.collect.update(document)

9.删除文档(document结构为json):
db.collect.remove(document)

10.查询文档(个人理解:pretty相当于sql里面的where或者过滤器):
db.collect.find()
db.collect.findOne()
db.collect.find().pretty()

db.collect.find().limit(长度)
db.collect.find().skip(数字);.skip(指定跳过数据个数)

db.collect.find().sort({key:1});(sort(key:1表示升序 -1表示降序):key表示排序的字段)

db.collect.find({$or: [{key1: value1}, {key2:value2}]}).pretty();(or条件)

通配符(类型见表):
| uble | 1 | |
| String | 2 | |
| Object | 3 | |
| Array | 4 | |
| Binary data | 5 | |
| Undefined | 6 | 已废弃。 |
| Object id | 7 | |
| Boolean | 8 | |
| Date | 9 | |
| Null | 10 | |
| Regular Expression | 11 | |
| JavaScript | 13 | |
| Symbol | 14 | |
| JavaScript (with scope) | 15 | |
| 32-bit integer | 16 | |
| Timestamp | 17 | |
| 64-bit integer | 18 | |
| Min key | 255 | Query with -1. |
| Max key | 127 |
11.添加索引:
db.collection.createIndex({"key":value})

12.聚合方法:
db.collection.aggregate(AGGREGATE_OPERATION)
关键字:
| 表达式 | 描述 | 实例 |
| $sum | 计算总和。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) |
| $avg | 计算平均值 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) |
| $min | 获取集合中所有文档对应值得最小值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) |
| $max | 获取集合中所有文档对应值得最大值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) |
| $push | 将值加入一个数组中,不会判断是否有重复的值。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) |
| $addToSet | 将值加入一个数组中,会判断是否有重复的值,若相同的值在数组中已经存在了,则不加入。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) |
| $first | 根据资源文档的排序获取第一个文档数据。 | db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) |
| $last | 根据资源文档的排序获取最后一个文档数据 | db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}]) |
函数对比(Mysql对比,方便记忆):
| 作/函数 | mongodb聚合操作 |
| where | $match |
| group by | $group |
| having | $match |
| select | $project |
| order by | $sort |
| limit | $limit |
| sum() | $sum |
| count() | $sum |
| join | $lookup |
4万+

被折叠的 条评论
为什么被折叠?



