seo做网站,高新区建设局网站,中国的外贸企业有哪些,三大网络架构一、前文回顾
上一篇文章中我们学习了MongoDB中的更新方法#xff0c;也学了一部分操作符。今天我们将学习最后一个操作“删除”。
二、删除
原始数据如下#xff1a;
1、deleteOne
语法#xff1a;db.collection.deleteOne( query , options ) 具体参…一、前文回顾
上一篇文章中我们学习了MongoDB中的更新方法也学了一部分操作符。今天我们将学习最后一个操作“删除”。
二、删除
原始数据如下
1、deleteOne
语法db.collection.deleteOne( query , options ) 具体参数参考官网 db.collection.deleteOne() 该命令会删除满足条件的第一个数据。 示例例如我们想删除item journal的数据
db.inventory.deleteOne({item:journal})2、deleteMany
语法db.collection.deleteMany( query , options ) 用法和上面基本一样有所不同的是 deleteMany会删除所有满足条件的数据这里不再赘述。
3、小结
至此我们已经把MongoDB的增删改查的基础学习完了接下来要学习的则是更进阶的部分。我们知道在大多数场景下数据库的作用是查询数据所有我们先学习查询的进阶内容。三、查询进阶—索引初步介绍
1、什么是索引
与绝大多数数据库一样MongoDB也有索引。索引是一种加速查询的数据结构通常是由B树或者B树。例如我们现在有一个100万个集合的文档我们要查找出其中name “hardy”的数据如果我们并没有在该字段中添加索引那么MongoDB默认会使用全表扫描的方式来查询如下图 如果没有索引那么MongoDB只能从第一个文档开始 挨个的比较数据是否匹配在数据量小的时候比较快但是随着数据量的增大很明显速度会越来越慢。那么该如何优化呢此时我们可以很容易的想到加索引。 索引可以简单理解为一个字典他记录了某个值以及该值对应的文档在哪里知道了具体的位置那么查询就会变的相当迅速。 B树的层级通常不高而且采用的是二分查找法。可以很大程度上减少查询的次数当找到对应索引之后索引上会记录文档的ID此时通过文档的ID再去查询文档效率就会大大提供。
2、怎么创建索引
语法db.collection.createIndex( keys , options , commitQuorum ) 完整介绍 db.collection.createIndex() 示例比如我要给集合inventory中的item字段添加索引
db.inventory.createIndex({item: 1},{unique: true,name: itemIndex}
)这里的1表示升序unique表示当前索引是唯一索引不能重复而name是索引的名称运行结果 从结果中可以看出之前存在一个索引现在存在两个索引。那么问题来了为什么一开始就存在一个索引呢这个其实也很好理解Mongo默认会给每个文档添加一个唯一的ID当作主键所以这个一开就存在的是主键索引。接下来我们看下结果是否和我们预期的一样。这里我们使用getIndexes命令
db.inventory.getIndexes()结果如下
3、怎么查看查询是否用了索引
和MySQL一样MongoDB中也有explain函数用于查看数据库的执行计划。这里我们用这个命令来判断当前的查询是否用了索引。
3.1、查询没有索引的字段
这里我们以status字段为例我们要查询statusA的数据
db.inventory.explain().find({status:A})部分结果如下 winningPlan表示实际使用的查询计划从这里不难看出针对当前查询MongoDB采用的是COLLSCAN集合扫描也可以理解为全表扫描需要每一个文档挨个的比较。
3.2、查询含有索引的列
db.inventory.explain().find({item:mat})部分结果如下 可以看到实际执行的计划是采用IXSCAN也就是索引扫描于此同时上面的stage还有一个FETCH这里的FETCH 也就是MySQL中的回表。因为当前的索引只记录了索引值以及当前索引对应的文档IDMongoDB拿到当前文档的ID后还需要通过ID再次去查一次文档。聪明的你可能会问了那有没有不用FECTH的情况呢很显然是有的比如我们只查询item字段此时就不需要再次回表。代码如下
db.inventory.explain().find({id:mat},{item:1})当然你可以直接通过ID查询也是不会回表的。至于为什么个放到后文讲
db.inventory.explain().find({_id:66951edbdf24000083007d75})结果如下
四、结束语
本文讲简单的讲述了一下删除文档操作以及对索引展开了一定的讲解当然索引的内容远不止此后面还有更多的内容希望对你有所帮助。
未完待续 文章转载自: http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.mgskc.cn.gov.cn.mgskc.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.nxhjg.cn.gov.cn.nxhjg.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.yrskc.cn.gov.cn.yrskc.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.nwfxp.cn.gov.cn.nwfxp.cn http://www.morning.yhwxn.cn.gov.cn.yhwxn.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.qqnjr.cn.gov.cn.qqnjr.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn http://www.morning.blqsr.cn.gov.cn.blqsr.cn http://www.morning.dwfxl.cn.gov.cn.dwfxl.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.ywqsk.cn.gov.cn.ywqsk.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.mrfr.cn.gov.cn.mrfr.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.xbhpm.cn.gov.cn.xbhpm.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn