MongoDB 教程
https://www.runoob.com/mongodb/mongodb-tutorial.html
目录
1、创建集合 db.createCollection(cln)
简介
MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。
MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。
SQL术语/概念 | MongoDB术语/概念 | 解释/说明 |
---|---|---|
database | database | 数据库 |
table | collection | 数据库表/集合 |
row | document | 数据记录行/文档 |
column | field | 数据字段/域 |
index | index | 索引 |
table joins | 表连接,MongoDB不支持 | |
primary key | primary key | 主键,MongoDB自动将_id字段设置为主键 |
数据库命名规则:
- 不能是空字符串("")。
- 不得含有' '(空格)、.、$、/、\和\0 (空字符)。
- 应全部小写。
- 最多64字节。
有一些数据库名是保留的,可以直接访问这些有特殊作用的数据库。
- admin: 从权限的角度来看,这是"root"数据库。要是将一个用户添加到这个数据库,这个用户自动继承所有数据库的权限。一些特定的服务器端命令也只能从这个数据库运行,比如列出所有的数据库或者关闭服务器。
- local: 这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合
- config: 当Mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息。
下表为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 | 正则表达式类型。用于存储正则表达式。 |
集合的命名规则:
- 集合名不能是空字符串""。
- 集合名不能含有\0字符(空字符),这个字符表示集合名的结尾。
- 集合名不能以"system."开头,这是为系统集合保留的前缀。
- 用户创建的集合名字不能含有保留字符。有些驱动程序的确支持在集合名里面包含,这是因为某些系统生成的集合中包含该字符。除非你要访问这种系统创建的集合,否则千万不要在名字里出现$。
需要注意的是:
- 文档中的键/值对是有序的。
- 文档中的值不仅可以是在双引号里面的字符串,还可以是其他几种数据类型(甚至可以是整个嵌入的文档)。
- MongoDB区分类型和大小写。
- MongoDB的文档不能有重复的键。
- 文档的键是字符串。除了少数例外情况,键可以使用任意UTF-8字符。
文档键命名规范:
- 键不能含有\0 (空字符)。这个字符用来表示键的结尾。
- .和$有特别的意义,只有在特定环境下才能使用。
- 以下划线"_"开头的键是保留的(不是严格要求的)。
一、数据库(database)
一个mongodb中可以建立多个数据库。默认数据库为"db",存储在data目录中。
1、查看数据库 show dbs
"show dbs" 命令:显示所有库的列表。
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
>
"db" 命令:显示当前的数据库对象或集合
> db
test
2、创建数据库 use db_name
"use"命令:连接到指定的数据库,如果数据库不存在,则创建数据库,否则切换到指定数据库。
> use local
switched to db local
> db
local
> use haha
switched to db haha
> db
haha
3、删除数据库 db.dropDatabase()
db.dropDatabase():删除当前数据库
> use haha
switched to db haha
> db
haha
> db.dropDatabase()
{ "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
>
4、备份数据库 mongodump
mongodump.exe -h 127.0.0.1:27017 -d two -o D:\beifen
C:\WINDOWS\system32>mongodump.exe -h 127.0.0.1:27017 -d two -o D:\beifen
2019-08-16T20:15:59.070+0800 writing two.data to
2019-08-16T20:15:59.098+0800 writing two.stu to
2019-08-16T20:15:59.101+0800 done dumping two.stu (10 documents)
2019-08-16T20:16:00.655+0800 done dumping two.data (500001 documents)
5、恢复数据库 mongorestore
mongorestore.exe -h 127.0.0.1:27017 -d six --dir D:\beifen\two
C:\WINDOWS\system32>mongorestore.exe -h 127.0.0.1:27017 -d six --dir D:\beifen\two
2019-08-16T20:21:57.589+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-08-16T20:21:57.629+0800 building a list of collections to restore from D:\beifen\two dir
2019-08-16T20:21:57.632+0800 reading metadata for six.data from D:\beifen\two\data.metadata.json
2019-08-16T20:21:57.633+0800 reading metadata for six.stu from D:\beifen\two\stu.metadata.json
2019-08-16T20:21:57.646+0800 restoring six.data from D:\beifen\two\data.bson
2019-08-16T20:21:57.659+0800 restoring six.stu from D:\beifen\two\stu.bson
2019-08-16T20:21:57.663+0800 no indexes to restore
2019-08-16T20:21:57.663+0800 finished restoring six.stu (10 documents)
2019-08-16T20:22:00.583+0800 [####....................] six.data 4.30MB/24.7MB (17.4%)
2019-08-16T20:22:03.583+0800 [########................] six.data 9.07MB/24.7MB (36.7%)
2019-08-16T20:22:06.583+0800 [#############...........] six.data 13.7MB/24.7MB (55.4%)
2019-08-16T20:22:09.583+0800 [#################.......] six.data 18.5MB/24.7MB (74.9%)
2019-08-16T20:22:12.583+0800 [######################..] six.data 23.2MB/24.7MB (94.0%)
2019-08-16T20:22:13.537+0800 [########################] six.data 24.7MB/24.7MB (100.0%)
2019-08-16T20:22:13.537+0800 no indexes to restore
2019-08-16T20:22:13.537+0800 finished restoring six.data (500001 documents)
2019-08-16T20:22:13.537+0800 done
6、导出文件 mongoexport
(1)导出json文件
mongoexport.exe -h 127.0.0.1:27017 -d six -c stu -o D:\beifen\data.json
C:\WINDOWS\system32>mongoexport.exe -h 127.0.0.1:27017 -d six -c stu -o D:\beifen\data.json
2019-08-16T21:07:24.670+0800 connected to: 127.0.0.1:27017
2019-08-16T21:07:24.708+0800 exported 10 records
(2)导出csv文件
mongoexport -h 127.0.0.1:27017 -d six -c stu -o D:\beifen\data1.csv --type csv -f _id,user,age,like
C:\WINDOWS\system32>mongoexport -h 127.0.0.1:27017 -d six -c stu -o D:\beifen\data1.csv --type csv -f _id,user,age,like
2019-08-16T21:10:35.583+0800 connected to: 127.0.0.1:27017
2019-08-16T21:10:35.617+0800 exported 10 records
7、导入文件 mongoimport
(1)导入json文件
mongoimport -h 127.0.0.1:27017 -d seven -c stu --file D:\beifen\data.json
C:\WINDOWS\system32>mongoimport -h 127.0.0.1:27017 -d seven -c stu --file D:\beifen\data.json
2019-08-16T21:18:59.171+0800 connected to: 127.0.0.1:27017
2019-08-16T21:18:59.226+0800 imported 10 documents
二、集合(collection)
集合就是 MongoDB 文档组,类似于 RDBMS (关系数据库管理系统:Relational Database Management System)中的表格。
集合存在于数据库中,集合没有固定的结构.
1、创建集合 db.createCollection(cln)
db.createCollection(name, options):创建集合,name: 要创建的集合名称;options可选参数, 指定有关内存大小及索引的选项;
> use two
switched to db two
> db.createCollection("stu")
{ "ok" : 1 }
>
2、查询集合 show collection
show collections 或 show tables 命令:
> show collections
stu
3、删除集合 db.cln.drop()
db.集合名.drop():删除集合
> show collections
col
hh
> db.hh.drop()
true
三、文档
文档是一组键值(key-value)对(即 BSON)。
{"site":"www.runoob.com", "name"