MongDB篇,第一章:数据库知识1

2023-06-14,,

MongDB    数据库知识1

程序 =  数据结构 + 算法

数据存储阶段

1,文件管理阶段 (.txt .doc .xls)

优点: 数据可以长期保存;可以存储大量的数据;使用简单
     缺点: 数据的一致性差;数据的查找修改不方便;数据的冗余性大

2,数据库存储阶段

优点 : 数据组织结构化,降低了冗余;方便扩展;方便程序调用,做自动化的处理
     缺点 : 数据库使用往往需要特定的语句或者命令进行操 作,相对比较复杂

几个概念

数据 :能够输入到计算机中并被识别处理的信息的集合;

数据结构:研究一个数据集合中,数据之间关系的学科;

数据库:按照数据结构,存储管理数据的仓库。 数据库是在数据库管理系统管理和控制下在一定介质上的 数据集合。

数据库管理系统:数据库管理软件,用于建立维护数据库;

数据库系统:由数据库和数据库管理系统等开发工具组成 的集合;

关系型数据库

  采用关系模型来组织数据结构的数据库 (二维表)

    Oracle DB2 SQLServer MySQL SqLite

优点:(1)容易理解,逻辑类似常见表格
      (2)使用方便,都使用sql语句,sql语句非常成熟
      (3)数据的一致性高,冗余低,完整性好
      (4)技术成熟,可以使用外部链接等复杂的操作

缺点:(1)每次都需要sql语句的解析,消耗较大
   (2)不能很好满足并发需求,特别是海量数据的爆发 ,关系型数据库普遍读写能力不足
   (3)关系型数据库内部每一步都要加锁的操作,以保 证操作的原子性,增加了数据库负担
   (4)数据的一致性有时会导致大量的空间浪费

数据库范式:

https://baike.baidu.com/item/%E6%95%B0%E6%8D%AE%E5%BA%93%E8%8C%83%E5%BC%8F/7309898?fr=aladdin

非关系型数据库 (NoSql --> not only sql)

优点 : (1)高并发,读写能力强;
     (2)普遍比关系型数据库容易扩展
     (3)弱化了数据结构,降低了数据的一致性

缺点 :(1) 通用性差,没有sql一样的一致化的操作
   (2)操作灵活,容易混乱;
   (3)没有join等操作,有的数据库不支持关系型数据库中类似事务等操作。版本更新快;

Nosql的使用情况:
  1.  数据一致性要求低
  2.  数据库处理海量并发存储
  3.  数据库大小不容易估算,需要分布扩展
  4.  给定的数据比较容易建立Nosql模型

Nosql的分类:

(1)键值型数据库 : Redis   oracle    BDB    tokyo
(2)列存储数据库 :Hbase
(3)文档型数据库 : MongoDB
(4)图形数据库:

MongoDB数据库 (非关系型数据库---> 文档型数据库)

  1.  由c++编写的数据库管理系统;
  2.  支持丰富的增删改查操作;
  3.  支持丰富的数据类型;
  4.  支持众多的编程语言接口(python ruby PHP c++ c#)
  5.  使用方便,便于部署。在非关系数据库中属于比较成熟 的数据库

MongoDB安装

自动安装:
sudo apt-get install mongodb

cookie

whereis 软件名称 : 查看软件位置

默认安装位置: /var/lib/mongodb
配置文件位置: /etc/mongodb.conf
命令集: /usr/bin /usr/local/bin

手动安装:
      1.下载MongoDB (开源)
www.mongodb.com--->get mongodb-->community server
选择想要的版本下载

2.选择合适的位置解压 (/usr/local /opt)
tar 解压后得到Mongodb文件夹

  3. 将MongoDB文件夹中的bin文件夹变为环境变量
PATH=$PATH:/opt/mongo..../bin
export PATH
将以上两句写入 /etc/rc.local 或者/etc/bash.bashrc 自动添加脚本

   4. 重启系统

reboot

source  /etc/bash.bashrc

  5, 设置数据库的存储位置:

sudo mkdir -p  /data/db

mongod --dbpath     /data/db

# cat /etc/rc.local | grep -v '#'

touch /var/lock/subsys/local

PATH=$PATH:/tmp/mongodb-linux-x86_64-4.0.0/bin
export PATH
[root@shenzhen ~]# source /etc/rc.local ^C
[root@shenzhen ~]# mongo
mongo mongoexport mongoreplay mongostat
mongod mongofiles mongorestore mongotop
mongodump mongoimport mongos

 

 6,设置数据库端口号:27017

    mongod  --port  8888

如果不设置默认是27017

  centos7.2安装mongodb

  转载:https://blog.csdn.net/junshangshui/article/details/79371316

mongo
  进入mongo shell界面 :mongodb的交互界面(主要:javaScript),用来通 过mongo语句操作mongodb数据库
退出mongo shell : quit() 或者 ctrl + c

  组织结构 : 键值对--》 文档 ---》 集合 --》数据库

  数据类型:字符串 、整型、布尔值、 浮点型、  数组类型、时间类型、文档类型、 空值null、字符串(symbol通常表示特殊字符)

时间cuo 、   ObjectID、 二进制、  代码、 正则表达式;

区别:

mysql                 mongo                  含义

database           database              数据库

table                  collection               表/集合

column                field                     字段/域

row                    document            记录/文档

index                     index                 索引

----------------------------------

id      |       name         |     age

------------------------------------

1      |            lucy       |     18

--------------------------------------

{

"_id": ObjectId("sgegrergtrhy123321htht"),

"name":"Lucy",

"age":18

},

{

"_id": ObjectId("sfsgdrsgy125671rtrttht"),

"name":"Lili",

"age":19

}

(1)创建数据库: use     databaseName

  e.g.   use stu

  * use 实际上是表示选择使用哪个数据库,如果这个数 据库不存在则表示同时创建这个数据库
  * 使用use后数据库不会被马上创建,而是需要写入数据 时才会创建

(2)查看当前系统下数据库: show   dbs

系统数据库:

  admin:  存放用户及其权限;

  local :  存储本地数据;

  config :  存储分片信息;

数据库命名规则:

  1,使用UTF-8字符; 2,不能含有空格,点   /    \    ‘\0’ 字符  ;3,长度不能超过64字节; 4,不能喝系统数据库重名; 5,习惯使用小写字母,表达数据库功能;

  db: mongo系统全局变量,代表当前正在使用的数据库;

  注: 当不用use选择任何数据库时,db表示test。此时插入数据则创建test数据库;

进入报错问题:
https://blog.csdn.net/wangli61289/article/details/44459467 #############
> use std
switched to db std
> db
std
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.std.insert({"name":"xixi"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
std 0.000GB
> db #代表当前正在使用的数据库
std
> use admin
switched to db admin
> db
admin
>
> db
test
> db.mm.insert({'mmtest':'hello'})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
std 0.000GB
test 0.000GB
>

(3)数据库的备份和恢复;

  备份:  mongodump   -h   dbhost    -d    dbname    -o   dbdir

  e.g.
  mongodump -h 127.0.0.1 -d stu -o bak
  将127.0.0.1主机上stu数据库备份入bak文件夹

[root@shenzhen ~]# mongodump -h 127.0.0.1 -d std -o /tmp/student
2018-07-10T21:11:27.186+0800 writing std.mm to
2018-07-10T21:11:27.232+0800 writing std.std to
2018-07-10T21:11:27.550+0800 done dumping std.mm (1 document)
2018-07-10T21:11:27.550+0800 done dumping std.std (1 document)
[root@shenzhen ~]# cd /tmp/
[root@shenzhen tmp]# cd student/
[root@shenzhen student]# ls
std
[root@shenzhen student]# cat std/
cat: std/: Is a directory
[root@shenzhen student]# cd std/
[root@shenzhen std]# ls
mm.bson mm.metadata.json std.bson std.metadata.json
[root@shenzhen std]#

  恢复: mongorestore   -h   <localhost>:<port>   -d    dbname   <path>

  e.g.
  mongorestore -h 127.0.0.1:27017 -d student bak/stu
  将bak文件夹下备份的stu数据库恢复到 本机的student数据库中。student不存在则会自动创建

[root@shenzhen ~]# mongorestore -h 127.0.0.1:27017 -d test /tmp/student/std/
2018-07-10T21:20:05.858+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
2018-07-10T21:20:05.874+0800 building a list of collections to restore from /tmp/student/std dir
2018-07-10T21:20:06.239+0800 reading metadata for test.mm from /tmp/student/std/mm.metadata.json
2018-07-10T21:20:06.240+0800 restoring test.mm from /tmp/student/std/mm.bson
2018-07-10T21:20:06.297+0800 reading metadata for test.std from /tmp/student/std/std.metadata.json
2018-07-10T21:20:06.397+0800 no indexes to restore
2018-07-10T21:20:06.397+0800 finished restoring test.mm (1 document)
2018-07-10T21:20:06.463+0800 restoring test.std from /tmp/student/std/std.bson
2018-07-10T21:20:06.494+0800 no indexes to restore
2018-07-10T21:20:06.494+0800 finished restoring test.std (1 document)
2018-07-10T21:20:06.494+0800 done
[root@shenzhen ~]#
> use test
switched to db test
> show tables #恢复前
mm
> show tables #恢复后
mm
std
>

(4)数据库的检测;

mongostat

insert (每秒插入次数)  query(每秒查询次数)  update(每秒更新次数)  delete(每秒删除次数) getmore command(每秒运行命令次数)

dirty  used flushes (每秒向磁盘写入次数)  vsize (使用虚拟内存情况)  res(使用物理内存情况)

qrw  arw  net_in   net_out    conn   time

[root@shenzhen ~]# mongostat
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 4|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 363b 138k 2 Jul 10 21:23:19.787
*0 *0 *0 *0 0 0|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 47b 18.0k 2 Jul 10 21:23:23.137
*0 *0 *0 *0 0 0|0 0.0% 0.0% 1 997M 54.0M 0|0 1|0 17b 6.48k 2 Jul 10 21:23:32.429
*0 *0 *0 *0 0 0|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 16b 4.60k 3 Jul 10 21:23:45.527
*0 *0 *0 *0 0 5|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 460b 175k 3 Jul 10 21:23:45.870
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 181b 69.1k 3 Jul 10 21:23:46.741
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 173b 66.0k 3 Jul 10 21:23:47.654
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 145b 55.4k 3 Jul 10 21:23:48.742
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 173b 66.0k 3 Jul 10 21:23:49.654
*0 *0 *0 *0 0 3|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 216b 60.6k 3 Jul 10 21:23:50.653
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.3k 3 Jul 10 21:23:51.652
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 154b 59.0k 3 Jul 10 21:23:52.673
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 161b 61.5k 3 Jul 10 21:23:53.652
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.4k 3 Jul 10 21:23:54.650
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.2k 3 Jul 10 21:23:55.649
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 156b 59.8k 3 Jul 10 21:23:56.657
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 159b 60.8k 3 Jul 10 21:23:57.647
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.1k 3 Jul 10 21:23:58.649
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 156b 59.8k 3 Jul 10 21:23:59.657
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 204b 58.3k 3 Jul 10 21:24:00.691
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 164b 62.7k 3 Jul 10 21:24:01.651
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.4k 3 Jul 10 21:24:02.649
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.0k 3 Jul 10 21:24:03.653
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.4k 3 Jul 10 21:24:04.650
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 155b 59.1k 3 Jul 10 21:24:05.669
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 160b 61.4k 3 Jul 10 21:24:06.650
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.0k 3 Jul 10 21:24:07.654
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.1k 3 Jul 10 21:24:08.655
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.4k 3 Jul 10 21:24:09.652
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.1k 3 Jul 10 21:24:10.655
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.2k 3 Jul 10 21:24:11.655
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.2k 3 Jul 10 21:24:12.656
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.3k 3 Jul 10 21:24:13.655
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 158b 60.5k 3 Jul 10 21:24:14.649
*0 *0 *0 *0 0 3|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 212b 60.3k 3 Jul 10 21:24:15.649
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.0k 3 Jul 10 21:24:16.653
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 155b 59.2k 3 Jul 10 21:24:17.670
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 161b 61.5k 3 Jul 10 21:24:18.650
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 997M 54.0M 0|0 1|0 157b 60.0k 3 Jul 10 21:24:19.654
^C2018-07-10T21:24:19.837+0800 signal 'interrupt' received; forcefully terminating
[root@shenzhen ~]#

mongotop   检测每个数据库的读写时长;

ns  数据表           total         read 读时长            write 写时长

[root@shenzhen ~]# mongotop
2018-07-10T21:42:38.828+0800 connected to: 127.0.0.1 ns total read write 2018-07-10T21:42:39+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:40+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:41+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:43+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:44+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:45+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:46+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:47+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:48+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:49+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:50+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ns total read write 2018-07-10T21:42:51+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
std.mm 0ms 0ms 0ms
std.std 0ms 0ms 0ms
test.mm 0ms 0ms 0ms
test.std 0ms 0ms 0ms ^C2018-07-10T21:42:52.104+0800 signal 'interrupt' received; forcefully terminating
[root@shenzhen ~]#

(5)删除数据库:

  db.dropDatabase()   删除db当前所代表的数据库

> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
std 0.000GB
test 0.000GB
> use test
switched to db test
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
std 0.000GB
>

(6)创建集合

  db.createCollection(collection_name)

  e.g. db.createCollection('class2')
  创建一个集合名字为class2

(7)查看数据库中的集合

  show  tables 或  show  collections

集合的命名规则:1,不能是空字符串; 2,不能含有‘\0’ ; 3,不能以system.开头,这是系统集合的保留前缀;

        4,集合不要和保留字段重名,不要包含$

(8)创建集合2

  当向一个集合中插入一条文档,如果这个集合不存在,则会自动创建集合;

  db.collection_name.insert()

  e.g. db.class3.insert({name:'Lucy'})
  如果class3不存在则自动创建这个集合;

> show collections
class1
std
> db.class3.insert({'name':'lili','age':19})
WriteResult({ "nInserted" : 1 })
> show tables
class1
class3
std
>

(9)删除集合

  db.collection_name.drop()

  e.g. db.class3.drop()
  删除class3这个集合

> show tables
class1
class3
std
> db.std.drop()
true
> show tables
class1
class3
>

(10)文档:

在MongoDB中数据的组建形式;

由键值组成,类似python中的字典;

bson  --->  json

MongoDB中的数据文档为bson格式;(使用键值的方式保存和展示数据)

文档中键的命名规则:1,utf-8 格式的字符串; 2,不能有\0  不能为空(习惯上不用 . 和$ )

          3,以_开头的很多都是保留的键, 所以一般不用_开头;

注意:文档中的健值对是有序的; 文档的值指的就是文档支持的数据类型; MongoDB区分大小写;

支持数据类型:

整型                          整型,       32位整型

布尔                          True              False

浮点型                        存储小数

Arrays       列表数组

Timestamp     时间cuo

Date        时间日期

Object           内部文档

Null         空值

Symbol       特殊字符字符串

Binary  data      二进制数据

code        代码

regex        正则表达式

ObjectID       ObjectId字串  

ObjectId  :系统自动为每个文档生成的不重复的主键;

键名:_id

值:ObjectId("5b28a02c154710728ef6cac2")

"_id" : ObjectId("5b28a02c154710728ef6cac2")

24位 16进制数 ----》保证_id值的唯一性;

8位文档创建时间  6位机器id  4位进程id  6位计数器;

集合中的文档:

1,集合中的文档不一定都有相同的域;

2,集合中的文档域的个数也不一定相同

集合设计原则:

1,同一个文档存放在一个集合中;

2,集合中尽量存储域和文档格式相近的文档;

3,集合中可以存放文档数据的差异;

(11)插入文档;

db.collection_name.insert()

* 插入操作是键可以不加引号
* 查看插入结果 db.class1.find()
* _id 为系统自动添加主键,如果自己写_id则会使用自 己的值,但是仍然不能重复

_id 为系统定义的主键值,如果使用系统值则保证不重复,如果自定义值即会覆盖系统值,但是自己也不能让该域重复;

> show tables
class1
class3
> db.class1.insert({name:'xiaoming',age: 20, sex:'m'})
WriteResult({ "nInserted" : 1 })
> db.class1.insert({name:'ttttt',age: 20, sex:'m'})
WriteResult({ "nInserted" : 1 })
> db.class1.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
>

注:插入文档时,域名(键)可以不加引号;

插入多条文档;

db.collectionName.insert([{},{},{}])

e.g.
db.class0.insert([{name:'阿花',age:28},{name:'阿彪',age:27},{name:'阿宝',age:'26'}])

> db.class1.insert([{name:'ttttt',age: 20, sex:'m'},{name:'aaa',age:18},{name:'bbb',age:21}])

BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
>

save 插入文档
db.collectionName.save()

e.g. db.class0.save({name:'阿荣',age:29})

*如果不加_id选项时save和insert相同
*如果加_id项,则如果此_id值存在则save表示修改该文 档内容,如果不存在则正常插入;

* save  不能够插入多条文档

> db.class1.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class1.insert([{name:'ttttt',age: 20, sex:'m'},{name:'aaa',age:18},{name:'bbb',age:21}]) BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
> db.class1.save({name:'xxxxx',age:11})
WriteResult({ "nInserted" : 1 })
> db.class1.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
> db.class1.save({_id:1,name:'mimi',age:19})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })
> db.class1.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
{ "_id" : 1, "name" : "mimi", "age" : 19 }
> db.class1.save({_id:1,name:'aiai',age:19})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
{ "_id" : 1, "name" : "aiai", "age" : 19 }
>

MongDB篇,第一章:数据库知识1的相关教程结束。

《MongDB篇,第一章:数据库知识1.doc》

下载本文的Word格式文档,以方便收藏与打印。