MongoDB的基本操作(插入、删除、更新、索引)

###一、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>
&emsp;&emsp;<font color=red size=4>语法:</font>`db.collectionName.drop()`
&emsp;&emsp;<font color=red size=4>说明:</font>
&emsp;&emsp;collectionName: 准备删除的集合名称。返回值 true 成功删除;false 反之。
&emsp;&emsp;<font color=red size=4>实例:</font>

db.test.drop()
true

<font color=#0099ff size=4 face="黑体">5.插入文档</font>
&emsp;&emsp;<font color=red size=4>语法:</font>`db.collectionName.insert(document)`
或`db.collectionName.save(document)`
&emsp;&emsp;<font color=red size=4>说明:</font>
&emsp;&emsp;collectionName: 为集合名称,document: 是一个json格式的字符串。
&emsp;&emsp;insert() 方法是插入一条不同的数据,_id相同会插入失败,但save() 方法在插入一条数据时,如果_id不一样则插入,如果_id一样则修改。
&emsp;&emsp;<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>
&emsp;&emsp;<font color=red size=4>语法:</font>`db.collection.update(<query>,<update>,<option>)`
&emsp;&emsp;<font color=red size=4>说明:</font>
&emsp;&emsp;query: update的查询条件,类似sql语句中where后面的;
&emsp;&emsp;update: update的对象和一些更新的操作符(如$set)等,也可以理解为sql中update内set后面的。
&emsp;&emsp;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>
&emsp;&emsp;<font color=red size=4>实例:</font>

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

<font color=#0099ff size=4 face="黑体">7.查询文档</font>
&emsp;&emsp;<font color=red size=4>语法:</font>`db.collection.find(query, projection)`
&emsp;&emsp;<font color=red size=4>说明:</font>
&emsp;&emsp;query:可选参数,使用查询操作符指定查询条件;
&emsp;&emsp;projection:可选参数,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。
&emsp;&emsp;<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>
&emsp;&emsp;<font color=red size=4>语法:</font>`db.collection.remove(<query>, <justOne>)`
&emsp;&emsp;<font color=red size=4>说明:</font>
&emsp;&emsp;query: 可选参数,删除的文档的条件;
&emsp;&emsp;justOne: 可选参数,如果设为 true 或 1,则只删除一个文档。如果不选参数则集合中全部文档都删除。
&emsp;&emsp;<font color=red size=4>实例:</font>

//按指定条件删除

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

//删除全部

db.test.remove({})

###三、MongoDB索引操作
<font color=#0099ff size=4 face="黑体">1.创建索引</font>
&emsp;&emsp;<font color=red size=4>语法:</font>`db.collection.createIndex(keys, options)`
&emsp;&emsp;<font color=red size=4>说明:</font>
&emsp;&emsp;在3.0版本之前的语法命令是` db.collection.ensureIndex()`,但现在也可以使用,但它是createIndex的别名。
&emsp;&emsp;keys: 为你要创建的索引字段,1 为指定按升序创建索引,-1 按降序来创建索引;
&emsp;&emsp;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>
&emsp;&emsp;<font color=red size=4>实例:</font>

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

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

db.test.getIndexes()

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

//删除指定集合的索引

db.test.dropIndex(“name_1”)

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

db.test.dropIndexes()

###四、结束语
&emsp;&emsp;如果想学好MongoDB就需要多练多想,光看是不够的。
&emsp;&emsp;不足点:文章中更新文档和查询文档还有很多很多操作。比较更新或查询文档中还有各种操作符,查询文档中还有各种函数,比如 limit() 、sort() 和 skip() 等等。。。其次索引在 find() 中强制使用不有涉及等等。 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lytao123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值