###一、MongoDB与Sql数据库概念上的区别
  MongoDB与SQL数据库有几个概念上的问题是不一样的,主要有sql数据库中的表(table)在MongoDB中叫集合(collection);sql数据库表中一行记录(row)在MongoDB中叫文档(document);MongoDB中的主键(_id)能自动创建,只有弄清楚这两个大的区别,那就可以开始MongoDB的操作了。
###二、MongoDB的基本操作
1.创建数据库
  语法:use databaseName   说明:如果数据库不存在,则创建数据库(需要往里面插数据才创建,不然退出去就没了);否则切换到指定数据库。
  实例:

> use test
switched to db test
> db
test
>

2.删除数据库
  语法:db.dropDatabase()   说明:删除当前数据库,默认为 test,要想删除指定数据库,需要先使用use databaseName切换到 databaseName 再进行删除,同时可以使用db命令查看当前数据库名。
  实例:

> use test
switched to db test
>

3.创建集合
  语法:db.createCollection(name, options)      或db.collectionName.insert(obj)   说明:
  collectionName: 创建集合的名称,MongoDB可以不用单独的创建集合,当往里面插文档时会自动创建集合;
  name: 创建的集合名称;
  options: 可选参数, 指定有关内存大小及索引的选项。参数见下表:

字段

类型

说明

capped

布尔

设置为 true 时创建固定集合且必须指定 size 参数。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。

autoIndexId

布尔

默认为 false。true:自动在 _id 字段创建索引。

size

数值

为固定集合指定一个最大值(单位字节)。

max

数值

指定固定集合中包含文档的最大数量(单位条)。

  

实例: ``` //第一个实例 > db.test.insert({"content" : "ceshi"}) >

//第二个实例

db.createCollection(“test”, { capped : false, autoIndexId : true, size : 1024102410, max : 999999 } )
{ “ok” : 1 }

<font color=#0099ff size=4 face="黑体">4.删除集合</font>
  <font color=red size=4>语法:</font>`db.collectionName.drop()`
  <font color=red size=4>说明:</font>
  collectionName: 准备删除的集合名称。返回值 true 成功删除;false 反之。
  <font color=red size=4>实例:</font>

db.test.drop()
true

<font color=#0099ff size=4 face="黑体">5.插入文档</font>
  <font color=red size=4>语法:</font>`db.collectionName.insert(document)`
或`db.collectionName.save(document)`
  <font color=red size=4>说明:</font>
  collectionName: 为集合名称,document: 是一个json格式的字符串。
  insert() 方法是插入一条不同的数据,_id相同会插入失败,但save() 方法在插入一条数据时,如果_id不一样则插入,如果_id一样则修改。
  <font color=red size=4>实例:</font>

//第一个实例

db.test.insert({_id:1,
name: ‘MongoDB基础教程’,
age:22,
author: ‘ouyang’
})

//第二个实例

db.test.save({_id:1,
name: ‘MongoDB基础教程’,
age:22,
author: ‘XXXXX’
})

<font color=#0099ff size=4 face="黑体">6.更新文档</font>
  <font color=red size=4>语法:</font>`db.collection.update(<query>,<update>,<option>)`
  <font color=red size=4>说明:</font>
  query: update的查询条件,类似sql语句中where后面的;
  update: update的对象和一些更新的操作符(如$set)等,也可以理解为sql中update内set后面的。
  option: 为可选参数,有如下三个:
<table>
<thead>
    <tr>
            <th>字段</th>
            <th width="50px">类型</th>
            <th align="center">说明</th>
    </tr>
</thead>
<tbody>
     <tr>
     <td>upsert</td>
     <td>布尔</td>
     <td align="left">在 update 不存在的记录时,设置 true 为插入,false 反之;默认是false。</td>
   </tr>
    <tr>
     <td>multi</td>
     <td>布尔</td>
     <td align="left"> 如果这个参数为true,就把按条件查出来多条记录全部更新;默认是 false 只更新找到的第一条记录。</td>
   </tr>
    <tr>
     <td>writeConcern</td>
     <td>数值</td>
     <td align="left">抛出异常的级别。</td>
   </tr>
</tbody>
</table>
  <font color=red size=4>实例:</font>

db.test.update({‘name’:‘MongoDB基础教程’},{$set:{‘age’:23}},{multi:true})

<font color=#0099ff size=4 face="黑体">7.查询文档</font>
  <font color=red size=4>语法:</font>`db.collection.find(query, projection)`
  <font color=red size=4>说明:</font>
  query:可选参数,使用查询操作符指定查询条件;
  projection:可选参数,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。
  <font color=red size=4>实例:</font>

//第一个实例

db.test.find()

//AND 实例

db.test.find({key1:value1, key2:value2})

//OR 实例

db.test.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
)

<font color=#0099ff size=4 face="黑体">8.删除文档</font>
  <font color=red size=4>语法:</font>`db.collection.remove(<query>, <justOne>)`
  <font color=red size=4>说明:</font>
  query: 可选参数,删除的文档的条件;
  justOne: 可选参数,如果设为 true 或 1,则只删除一个文档。如果不选参数则集合中全部文档都删除。
  <font color=red size=4>实例:</font>

//按指定条件删除

db.test.remove({‘name’:‘MongoDB基础教程’})

//删除全部

db.test.remove({})

###三、MongoDB索引操作
<font color=#0099ff size=4 face="黑体">1.创建索引</font>
  <font color=red size=4>语法:</font>`db.collection.createIndex(keys, options)`
  <font color=red size=4>说明:</font>
  在3.0版本之前的语法命令是` db.collection.ensureIndex()`,但现在也可以使用,但它是createIndex的别名。
  keys: 为你要创建的索引字段,1 为指定按升序创建索引,-1 按降序来创建索引;
  options: 为可选参数,下面列举几个常用的:
<table>
<thead>
    <tr>
            <th>字段</th>
            <th width="50px">类型</th>
            <th align="center">说明</th>
    </tr>
</thead>
<tbody>
     <tr>
     <td>unique</td>
     <td>布尔</td>
     <td align="left">建立的索引是否唯一。指定为true创建唯一索引。默认值为false。</td>
   </tr>
    <tr>
     <td>name</td>
     <td>布尔</td>
     <td align="left"> 索引的名称。如果未指定,MongoDB的通过连接索引的字段名和排序顺序生成一个索引名称。</td>
   </tr>
    <tr>
     <td>background</td>
     <td>布尔</td>
     <td align="left">建索引过程会阻塞其它数据库操作,background可指定以后台方式创建索引,即增加 "background" 可选参数。 "background" 默认值为false。</td>
   </tr>
       <tr>
     <td>expireAfterSeconds</td>
     <td>数值</td>
     <td align="left">设定集合的生存时间,单位(秒)。</td>
   </tr>
</tbody>
</table>
  <font color=red size=4>实例:</font>

db.test.createIndex({name: 1, age: 1}, {background: true})

<font color=#0099ff size=4 face="黑体">2.查询索引</font>
  <font color=red size=4>语法:</font>`db.test.getIndexes()`
  <font color=red size=4>说明:</font>查看集合的所有索引。
  <font color=red size=4>实例:</font>

db.test.getIndexes()

<font color=#0099ff size=4 face="黑体">3.删除索引</font>
  <font color=red size=4>语法:</font>`db.users.dropIndex(indexName)`或`db.users.dropIndexes()`
  <font color=red size=4>说明:</font>
  indexName: 索引名称;
  dropIndex(): 方法用于删除指定的索引;
  dropIndexes(): 方法用于删除全部的索引。
  <font color=red size=4>实例:</font>

//删除指定集合的索引

db.test.dropIndex(“name_1”)

//删除集合test的全部索引

db.test.dropIndexes()

###四、结束语
  如果想学好MongoDB就需要多练多想,光看是不够的。
  不足点:文章中更新文档和查询文档还有很多很多操作。比较更新或查询文档中还有各种操作符,查询文档中还有各种函数,比如 limit() 、sort() 和 skip() 等等。。。其次索引在 find() 中强制使用不有涉及等等。