MongoDB基础操做

更多内容欢迎来到博客 :https://imjianjian.github.io


MongoDB版本:3.4
git

查询文档

find()github

查询全部

db.collection.find({})
db.collection.find()

文档查询

//查询name="jianjian"的全部文档
db.collection.find({"collection":"jianjian"})

条件操做符

  • $lt 小于
  • $gt 大于
  • $gte 大于等于
  • $lte 小于等于
  • $ne 不等于
  • $all 全包含
  • $in 包含
  • $nin 不包含
//查询年龄小于18的
db.collection.find("age":{$lt:"18"})

$all

必须知足全部值sql

//能够查询出来{age:[20,21,22]}可是查询不出来{age:[20,22,23]},即必定要有20和21.
db.collection.find({age:{$all:[20,21]}});

$in

这个与$all不同,查询的值在$in给出的范围以内就均可以查出来。mongodb

//{age:[20,21,22]},{age:[20,22,23]}都能查出来
db.collection.find({age:{$in:[20,21]}});

$exists

判断key是否存在数据库

判断age字段是否存在
db.collection.find({age:{$exists:true}});

null值的处理

null值处理须要注意的是,不单单能够查询出来某一字段值为null的记录,还能够查出来不存在某一字段的记录。数组

db.collection.find({age:null})

能够查询出来age为null的记录,以及没有age字段的记录。若是咱们须要只去查询存在age字段而且age字段的值为null的记录,须要配合exists操做。数据结构

db.collection.find({age:{"$in":[null],"$exists":true}});

限制查询条数

limit(number)性能

number为条数网站

//查询条数限制为10条
db.collection.find().limit(10)

排序

sort({key:1})code

key为须要排序的字段,升序为1,降序为-1

//按照年龄升序排序
db.collection.find().sort({"age":1})

跳过记录

skip()

查询第10到20条的记录
db.collection.find().skip(10).limit(10);

查询文档条数

count()

age大于25的条数
db.collection.find({age: {$gte: 25}}).count();

插入文档

insert()基本语法以下:

db.collection.insert(
   <document or array of documents>,
   {
     writeConcern: <document>,
     ordered: <boolean>
   }
)

单条插入

db.collection.insert({"name":"jianjian"})

多条插入

db.collection.insert([{"name":"jianjian"},{"name":"dongdong"}])

错误的语法

db.collection.insert({"age":11},{"age":12})

只有age:11被插入进去,因为接收的插入文档不是数组

容易误导的地方:

db.collection.insert([{"name":"jianjian"},{"name":"dongdong"}])

db.collection.insert({"name":"jianjian"},{"age":"dongdong"})

db.collection.insert({"name":"jianjian","name":"dongdong"})

第一个插入是插入两个文档,第二个插入是插入一个文档,第三个虽然也是一个文档可是因为键重复,因此只有后一个值会被插入age:12

删除文档

remove() 方法的基本语法格式以下所示:

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }

参数说明:

  • query :(可选)删除的文档的条件。
  • justOne : (可选)若是设为 true 或 1,则只删除一个文档。
  • writeConcern :(可选)抛出异常的级别。

删除num大于100的数据

db.new.remove({"num":{$gt:100}});

删除new集合全部数据

db.new.remove({});

删除num等于100的数据

db.new.remove({"num":100})

更新文档

update() 方法用于更新已存在的文档。语法格式以下:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

参数说明:

  • query : update的查询条件,相似sql update查询内where后面的。
  • update : update的对象和一些更新的操做符(如$,$inc...)等,也能够理解为sql update查询内set后面的

upsert : 可选,这个参数的意思是,若是不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。

  • multi : 可选,mongodb 默认是false,只更新找到的第一条记录,若是这个参数为true,就把按条件查出来多条记录所有更新。
  • writeConcern :可选,抛出异常的级别。

$set修改符

用于修改键的值,若是键不存在就增长键

//将age=10的数据改为15,默认若是age=10的有多条记录只更新第一条
db.collection.update({"name":"li"},{$set:{"age":10}})

//更新多个知足条件的值,同时若是更新的值不存在就插入更新的值,注意:这里插入的值是20不是30
db.collection.update({"age":30},{$set:{"age":20}},{multi:true,upsert:true})

能够省略multi,upsert,这里不能有花括号,可是不建议这样作
db.collection.update({"age":30},{$set:{"age":20}},true,true)

//值更新为数组
db.collection.update({"name":"zhang"},{$set:{"age":[10,
12,14]}},{upsert:true})

//修改成其它的值
db.collection.update({"name":"zhang"},{$set:{"age":''}},{upsert:true})
db.collection.update({"name":"zhang"},{$set:{"age":null}},{upsert:true})

$inc修改符

用于增长已有键的值,若是键不存在就建立,只能用于整形、长整型、浮点型。

//将name=zhang的记录的age键+10
db.collection.update({"name":"zhang"},{$inc:{"age":10}},{upsert:true})

//将name=zhang的记录的age键-10
db.collection.update({"name":"zhang"},{$inc:{"age":-10}},{upsert:true})

$unset修改符

删除键相似关系数据库的删除字段操做,要区别$set修改符的将键设空或者null值

db.collection.update({"name":"zhang"},{$unset:{"age":1}})

$push修改符

若是数组已经存在,“$push”会向已有的数组末尾加入一个元素,要是没有就建立一个新的数组。注意:必须是数组才能加入新的值

db.collection.update({"name":"zhang"},{$push:{"comments":{"email":"abc@qq.com","address":"beijing"}}});

//再插入一条comments
db.collection.update({"name":"zhang"},{$push:{"comments":{"mark":"aaa"}}});

$each修改符

向数组中添加元素

db.collection.insert({"title":1,"list":[1,2,3]})

//向list数组中添加单个元素
db.collection.update({"title":1},{$push:{"list":4}})

//向list数组中添加多个元素
db.collection.update({"title":1},{$push:{"list":{$each:[6,7]}}});

$addToSet修改符

往数组集中插入元素时,若是元素存在就不插入;方法和$push同样,惟一的区别就是$push不会判断重复值,重复也能够插入。$addToSet也能够结合$each一块儿使用方法和$push同样能够同时往数组中插入多个元素而且若是元素存在则不插入。

db.collection.update({"title":1},{$addToSet:{"list":2}})

db.collection.update({"title":1},{$addToSet:{"list":{$each:[2,3,4]}}});

$pull修改符

匹配数组中的元素将匹配上的元素所有删除,注意只能对数组中的元素进行匹配

db.collection.insert({"title":1,"list":[1,2,3]});

//清除list数组中的元素2
db.collection.update({},{$pull:{"list":2}})

$pop修改符

从数组的末端或头删除一个元素,$pop:{"list":1}:从末尾删除一个元素;$pop:{"list":-1}:从开头删除一个元素

//向数组list中插入两个元素
db.collection.update({},{$push:{"list":{$each:[2,4]}}});

//从末尾删除元素
db.collection.update({},{$pop:{"list":1}});

//从开头删除元素
db.collection.update({},{$pop:{"list":-1}});

基于位置的数组修改方法
能够有两种方式来定位数组中的元素:

1.经过下标;数组的下标都是从0开始。

2.经过$定位操做符,

db.comment.insert({"title":1,"comments":[{"comment":"a","author":"chen","age":10},{"comment":"b","author":"wang","age":30},{"comment":"c","author":"zhang","age":40}]});
//将数组中的第一个列表的age值改为20
//方法1:
db.comment.update({},{$set:{"comments.0.age":20}});

//方法2:
db.comment.update({"comments.author":"chen"},{$set:{"comments.$.age":20}});

索引

索引一般可以极大的提升查询的效率,若是没有索引,MongoDB在读取数据时必须扫描集合中的每一个文件并选取那些符合查询条件的记录。

这种扫描全集合的查询效率是很是低的,特别在处理大量的数据时,查询能够要花费几十秒甚至几分钟,这对网站的性能是很是致命的。

索引是特殊的数据结构,索引存储在一个易于遍历读取的数据集合中,索引是对数据库表中一列或多列的值进行排序的一种结构

ensureIndex() 方法

MongoDB使用 ensureIndex() 方法来建立索引。

设置No为索引:

db.collection.ensureIndex({"No":1})
1为指定按升序建立索引,若是你想按降序来建立索引指定为-1便可。

删除集合

删除test集合,注意drop后面的括号里面不须要带花括号

db.test.drop()
相关文章
相关标签/搜索