MongoDB版本:3.4
git
find()github
db.collection.find({}) db.collection.find()
//查询name="jianjian"的全部文档 db.collection.find({"collection":"jianjian"})
//查询年龄小于18的 db.collection.find("age":{$lt:"18"})
必须知足全部值sql
//能够查询出来{age:[20,21,22]}可是查询不出来{age:[20,22,23]},即必定要有20和21. db.collection.find({age:{$all:[20,21]}});
这个与$all不同,查询的值在$in给出的范围以内就均可以查出来。mongodb
//{age:[20,21,22]},{age:[20,22,23]}都能查出来 db.collection.find({age:{$in:[20,21]}});
判断key是否存在数据库
判断age字段是否存在 db.collection.find({age:{$exists:true}});
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> }
参数说明:
db.new.remove({"num":{$gt:100}});
db.new.remove({});
db.new.remove({"num":100})
update() 方法用于更新已存在的文档。语法格式以下:
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
参数说明:
upsert : 可选,这个参数的意思是,若是不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
用于修改键的值,若是键不存在就增长键
//将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})
用于增长已有键的值,若是键不存在就建立,只能用于整形、长整型、浮点型。
//将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})
删除键相似关系数据库的删除字段操做,要区别$set修改符的将键设空或者null值
db.collection.update({"name":"zhang"},{$unset:{"age":1}})
若是数组已经存在,“$push”会向已有的数组末尾加入一个元素,要是没有就建立一个新的数组。注意:必须是数组才能加入新的值
db.collection.update({"name":"zhang"},{$push:{"comments":{"email":"abc@qq.com","address":"beijing"}}}); //再插入一条comments db.collection.update({"name":"zhang"},{$push:{"comments":{"mark":"aaa"}}});
向数组中添加元素
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]}}});
往数组集中插入元素时,若是元素存在就不插入;方法和$push同样,惟一的区别就是$push不会判断重复值,重复也能够插入。$addToSet也能够结合$each一块儿使用方法和$push同样能够同时往数组中插入多个元素而且若是元素存在则不插入。
db.collection.update({"title":1},{$addToSet:{"list":2}}) db.collection.update({"title":1},{$addToSet:{"list":{$each:[2,3,4]}}});
匹配数组中的元素将匹配上的元素所有删除,注意只能对数组中的元素进行匹配
db.collection.insert({"title":1,"list":[1,2,3]}); //清除list数组中的元素2 db.collection.update({},{$pull:{"list":2}})
从数组的末端或头删除一个元素,$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在读取数据时必须扫描集合中的每一个文件并选取那些符合查询条件的记录。
这种扫描全集合的查询效率是很是低的,特别在处理大量的数据时,查询能够要花费几十秒甚至几分钟,这对网站的性能是很是致命的。
索引是特殊的数据结构,索引存储在一个易于遍历读取的数据集合中,索引是对数据库表中一列或多列的值进行排序的一种结构
MongoDB使用 ensureIndex() 方法来建立索引。
设置No为索引:
db.collection.ensureIndex({"No":1}) 1为指定按升序建立索引,若是你想按降序来建立索引指定为-1便可。
删除test集合,注意drop后面的括号里面不须要带花括号
db.test.drop()