一、CURD
1. 增加
单条文档
 | db.collection.insert() { 	<document> }
  第一种用法: db.user.insert({     name: "wu",     age: 20 });
 
  | 
 
多条文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | db.collection.insert() { 	[         <document 1>,         <document 2>,         {         	writeConcern: <bit>,         	ordered: <boolean>         }     ]	 }
  第二种用法:db.user.insert([     {name: "wu", age: 20},     {name: "wu1", age: 21} ]);
 
  | 
 
参数说明
- writeConcern: 写入策略,默认为 
1 ,即要求确认写操作,0 是不要求 
- ordered: 指定是否按顺序写入,默认为 
ture 按顺序写入 
脚本方式
1 2 3 4 5
   | for (let i = 0; i < 100; i++) { 	db.user.insert({ 		...... 	}) }
 
  | 
 
1 2 3 4 5 6 7
   | db.集合名.insert(),功能涵盖 insertOne() 和 insertMany()
  db.集合名.insertOne() 同 db.集合名.insert() 的第一种用法
  db.集合名.insertMany() 同 db.集合名.insert() 的第二种用法
 
  | 
 
2. 删除
1 2 3 4 5 6 7
   | db.集合名.remove( 	<query>     {     	justOne: <boolean>,     	writeConcern: <document>     } )
 
  | 
 
3. 更改
1 2 3 4 5 6 7 8 9
   | db.collection.update( 	<query>, 	<update>, 	{ 		upsert: <boolean>, 		multi: <boolean>, 		writeConcern: <document> 	} )
 
  | 
 
参数说明
- query: 条件,类似 
sql 中 update 语句后的 where 
- update: 更新内容,类似 
sql 中 update 语句后的 set 
- upsert: 可选,如果不存在 
update 的记录,是否插入 objNew,true 为插入,默认为 false 
- multi: 可选,默认是 
false, 只更新找到的第一条记录;如果 true,则更新所有匹配条件的值 
- writeConcern: 可选,抛出异常的级别
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
   | db.collection.update(	## 相当于先删除后更新,把符合条件的内容删除后,更新成语句中的 <document>     ## 删除第一条 name 为 zhangsan 的数据,替换为 name 为 lusi,age 为 21     {	 		name: "zhangsan" 	},     {     	name: "lusi",     	age: 21     } )
  db.collection.update(	## 保留原来的数据更新,但是只更新符合条件的第一条数据     ## 更新第一条 name 为 zhangsan 的数据,在保留其他数据的情况下更新 name 为 xiaoming     { 		name: "zhangsan", 	},     {     	$set:     	{     		name: "xiaoming"     	}     } )
  db.collection.update(	## 保留原来的数据更新,并且更新符合条件的所有数据     ## 更新所有zhangsan 的数据,在保留其他数据的情况下更新 name 为 xiaoming     { 		name: "zhangsan", 	},     {     	$set:     	{     		name: "xiaoming"     	}     }     multi: true,     upsert: true )
 
  | 
 
4. 查询
1
   | pretty() 方法以格式化的方式显示所有文档
 
  | 
 
二、索引
1. 创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | db.collection.createIndex(key, options) 如: db.collection.createIndex(     {     	title: 1,	## 为 title 字段创建升序索引     } )
  又如: db.collection.createIndex( 	{     	name:1	## 为 name 字段创建升序索引     },     {     	name: 'name_index',	## 指定索引名为 name_index     	expireAfterSecondes: 15,	## 指定索引的存在 15s 的过期时间     	unique: true	## 指定 name 为唯一索引     } )
  复合索引:一个索引的值是由多个 key 进行维护的索引 db.collection.createIndex(     {     	name: 1,	## 为 name 字段创建升序索引     	age: -1		## 为 age 字段创建降序索引     },     {     	name: 'name_age_index'     } )
 
  | 
 
说明:语法中 key 值为你要创建的索引字段,1 为升序创建索引,-1 为降序
2. 删除
1
   | db.collections.dropIndexes();
 
  | 
 
指定
1
   | db.collections.dropIndex("索引名称")
 
  | 
 
3. 查看
1
   | db.collection.getIndexes()
 
  | 
 
大小
1
   | db.collection.totalIndexSize();
 
  | 
 
三、聚合查询
类似于 SQL 中的 count(*),主要用于处理数据(诸如求平均值,求和等)
1 2 3 4 5 6 7 8 9 10
   | db.collection.aggregate([ 	{ 		$group: { 			_id: "$by_user", 			num_tutorial: { 				$sum: 1 			} 		} 	} ])
 
  |