🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
一、操作数据的4个基本操作 CRUD * 创建 > postblog={ "title":"My Blog","content":"here's my blog post" } { "title" : "My Blog", "content" : "here's my blog post" } > db.blog.insert(postblog) WriteResult({ "nInserted" : 1 }) * 读取 > db.blog.findOne() { "_id" : ObjectId("5a9653d176bf72afc4910a71"), "title" : "My Blog", "content" : "here's my blog post" } * 更新 ~~~ > single=db.person.findOne() { "_id" : ObjectId("5a96479666a9aa3f85f445dd"), "username" : "louis", "age" : 27 } > single.age=30 30 > db.person.update({"age":27},single) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.person.findOne( ) { "_id" : ObjectId("5a96479666a9aa3f85f445dd"), "username" : "louis", "age" : 30 } ~~~ 案例: ~~~ update命令 update命令格式: db.collection.update(criteria,objNew,upsert,multi) 参数说明: criteria:查询条件 objNew:update对象和一些更新操作符 upsert:如果不存在update的记录,是否插入objNew这个新的文档,true为插入,默认为false,不插入。 multi:默认是false,只更新找到的第一条记录。如果为true,把按条件查询出来的记录全部更新。 把count大于30的class name修改为c6 > db.classes.update({"count":{$gt:30}},{$set:{"name":"c8"}},false,true) WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 }) > db.classes.find() { "_id" : ObjectId("5a965a6dec5536ed2bd6742d"), "name" : "c6", "count" : 30 } { "_id" : ObjectId("5a965a72ec5536ed2bd6742e"), "name" : "c8", "count" : 40 } { "_id" : ObjectId("5a965a77ec5536ed2bd6742f"), "name" : "c8", "count" : 50 } { "_id" : ObjectId("5a965b1bec5536ed2bd67430"), "name" : "c8", "count" : 50 } ~~~ * 删除 ~~~ > db.person.remove({"age":50}) WriteResult({ "nRemoved" : 1 }) > db.person.find() { "_id" : ObjectId("5a9657557e8b849ebbb5e7f6"), "name" : "louis01", "age" : 21 } { "_id" : ObjectId("5a96575d7e8b849ebbb5e7f8"), "name" : "louis02", "age" : 32 } ~~~ 其他: ~~~ > db.stats() { "db" : "person", "collections" : 3, "views" : 0, "objects" : 6, "avgObjSize" : 5480.5, "dataSize" : 32883, "storageSize" : 86016, "numExtents" : 0, "indexes" : 3, "indexSize" : 86016, "fsUsedSize" : 3536478208, "fsTotalSize" : 13394386944, "ok" : 1 } ~~~ 第二部分:数据类型 1、BSON相对JSON表现出来的多余类型 JSON支持的类型,BSON这边也支持 * 函数 ~~~ > db.person.insert({"name":"alex",callback:function(){alert("1")}}) WriteResult({ "nInserted" : 1 }) > db.person.find() { "_id" : ObjectId("5a9657557e8b849ebbb5e7f6"), "name" : "louis01", "age" : 21 } { "_id" : ObjectId("5a96575d7e8b849ebbb5e7f8"), "name" : "louis02", "age" : 32 } { "_id" : ObjectId("5a965eb8ec5536ed2bd67431"), "name" : "alex", "callback" : { "code" : "function (){alert(\"1\")}" } } ~~~ * 日期类型 > var d=new Date() > d ISODate("2018-02-28T07:52:33.646Z") > db.person.insert({"name":"pack","date":d}) WriteResult({ "nInserted" : 1 }) > db.person.find() { "_id" : ObjectId("5a9657557e8b849ebbb5e7f6"), "name" : "louis01", "age" : 21 } { "_id" : ObjectId("5a96575d7e8b849ebbb5e7f8"), "name" : "louis02", "age" : 32 } { "_id" : ObjectId("5a965eb8ec5536ed2bd67431"), "name" : "alex", "callback" : { "code" : "function (){alert(\"1\")}" } } { "_id" : ObjectId("5a966022ec5536ed2bd67432"), "name" : "pack", "date" : ISODate("2018-02-28T07:52:33.646Z") } 三)CURD四种操作 * find命令 > for(var i=1;i<100;i++){ ... db.person.insert({"name":"hcx"+i,"age":i/19}) ... } WriteResult({ "nInserted" : 1 }) > db.person.count() 99 查找hcx10 > db.person.find({"name":"hcx10"}) { "_id" : ObjectId("5a9667ef531592b450664955"), "name" : "hcx10", "age" : 0.5263157894736842 } 指定返回的键 > db.person.find({"name":"hcx9"},{"age":1}) { "_id" : ObjectId("5a9667ef531592b450664954"), "age" : 0.47368421052631576 } > db.person.find({"name":"hcx9"},{"age":1,"_id":0}) { "age" : 0.47368421052631576 } 上面的0和1表示不展示或展示,1表示展示 ![](https://box.kancloud.cn/a30591690d1f0ec314218e749044e77c_527x161.png) 查询条件: * $lt、$lte、$gt、$gte $ne 不等于 > db.person.find({"age" : {"$gt":10,"$lte":15}}) { "_id" : ObjectId("5a966b233d47183f800349b0"), "name" : "alex6", "age" : 11 } { "_id" : ObjectId("5a966b233d47183f800349b1"), "name" : "alex7", "age" : 12 } { "_id" : ObjectId("5a966b233d47183f800349b2"), "name" : "alex8", "age" : 13 } { "_id" : ObjectId("5a966b233d47183f800349b3"), "name" : "alex9", "age" : 14 } { "_id" : ObjectId("5a966b233d47183f800349b4"), "name" : "alex10", "age" : 15 } $in 如果一个键有多个值与其匹配的话,就要用$in加一个条件数组 > db.person.find({"age":{"$in":[10,12,13]}}) { "_id" : ObjectId("5a966b233d47183f800349af"), "name" : "alex5", "age" : 10 } { "_id" : ObjectId("5a966b233d47183f800349b1"), "name" : "alex7", "age" : 12 } { "_id" : ObjectId("5a966b233d47183f800349b2"), "name" : "alex8", "age" : 13 } $or 如果想找出年龄是20或者name为alex50的 ~~~ > db.person.find({"$or":[{"age":20},{"name":"alex50"}]}) { "_id" : ObjectId("5a966b233d47183f800349b9"), "name" : "alex15", "age" : 20 } { "_id" : ObjectId("5a966b233d47183f800349dc"), "name" : "alex50", "age" : 55 } > db.person.find({"$or":[{"age":{"$in":[20,21,23]}},{"name":"alex50"}]}) { "_id" : ObjectId("5a966b233d47183f800349b9"), "name" : "alex15", "age" : 20 } { "_id" : ObjectId("5a966b233d47183f800349ba"), "name" : "alex16", "age" : 21 } { "_id" : ObjectId("5a966b233d47183f800349bc"), "name" : "alex18", "age" : 23 } { "_id" : ObjectId("5a966b233d47183f800349dc"), "name" : "alex50", "age" : 55 } ~~~ > var cond={"$or":[{"age":{"$gte":10,"$lte":15}},{"name":{"$in":["alex50","alex52","alex60"]}}]} > db.person.find(cond) { "_id" : ObjectId("5a966b233d47183f800349af"), "name" : "alex5", "age" : 10 } { "_id" : ObjectId("5a966b233d47183f800349b0"), "name" : "alex6", "age" : 11 } { "_id" : ObjectId("5a966b233d47183f800349b1"), "name" : "alex7", "age" : 12 } { "_id" : ObjectId("5a966b233d47183f800349b2"), "name" : "alex8", "age" : 13 } { "_id" : ObjectId("5a966b233d47183f800349b3"), "name" : "alex9", "age" : 14 } { "_id" : ObjectId("5a966b233d47183f800349b4"), "name" : "alex10", "age" : 15 } { "_id" : ObjectId("5a966b233d47183f800349dc"), "name" : "alex50", "age" : 55 } { "_id" : ObjectId("5a966b233d47183f800349de"), "name" : "alex52", "age" : 57 } { "_id" : ObjectId("5a966b233d47183f800349e6"), "name" : "alex60", "age" : 65 } $not 用在其他条件之上的,not操作符不能独立使用,必须配合其他的操作符使用 > db.person.find({"age":{"$not":{"$in":[10,11,12]}}}) * 查看数组 ~~~ > db.food.insert({"fruit":["apple","banana","peach"]}) WriteResult({ "nInserted" : 1 }) > db.food.find({"fruit":"banana"}) { "_id" : ObjectId("5a9678df1c1456e8f9540021"), "fruit" : [ "apple", "banana", "peach" ] } > db.food.insert({"fruit":["apple","kumquat","orange"]}) WriteResult({ "nInserted" : 1 }) > db.food.insert({"fruit":["cheery","banana","apple"]}) WriteResult({ "nInserted" : 1 }) > db.food.find() { "_id" : ObjectId("5a9678df1c1456e8f9540021"), "fruit" : [ "apple", "banana", "peach" ] } { "_id" : ObjectId("5a9679531c1456e8f9540022"), "fruit" : [ "apple", "kumquat", "orange" ] } { "_id" : ObjectId("5a9679751c1456e8f9540023"), "fruit" : [ "cheery", "banana", "apple" ] } > db.food.find({"fruit":{$all:["apple","banana"]}}) { "_id" : ObjectId("5a9678df1c1456e8f9540021"), "fruit" : [ "apple", "banana", "peach" ] } { "_id" : ObjectId("5a9679751c1456e8f9540023"), "fruit" : [ "cheery", "banana", "apple" ] } ~~~ 五)插入 * 批量插入 db.person.insert() 判断一个key是否存在 > db.person.find({"name":{ $exists: true}}) 删除 > show collections blog classes foo.batchinsert food person study > db.blog.drop() 删除collections true > db.study.drop() true > show collections classes foo.batchinsert food person > show dbs admin 0.000GB config 0.000GB foo 0.000GB local 0.000GB person 0.000GB 六)更新 ~~~ > db.foo.insert({"name":"hxc","age":25}) WriteResult({ "nInserted" : 1 }) > db.foo.update({"age":25},{"age":30}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find() { "_id" : ObjectId("5a97a53dc51b3387784da132"), "age" : 30 } "$set"修改器 > db.foo.insert({"name":"joe","age":25}) WriteResult({ "nInserted" : 1 }) > db.foo.insert({"name":"alex","age":20}) WriteResult({ "nInserted" : 1 }) > db.foo.insert({"name":"yuki","age":20}) WriteResult({ "nInserted" : 1 }) > db.foo.insert({"name":"louis","age":20}) WriteResult({ "nInserted" : 1 }) > db.foo.find() { "_id" : ObjectId("5a97a6d0c51b3387784da133"), "name" : "joe", "age" : 25 } { "_id" : ObjectId("5a97a6dec51b3387784da134"), "name" : "alex", "age" : 20 } { "_id" : ObjectId("5a97a6e2c51b3387784da135"), "name" : "yuki", "age" : 20 } { "_id" : ObjectId("5a97a6e7c51b3387784da136"), "name" : "louis", "age" : 20 } > db.foo.update({"name":"alex"},{"$set":{"age":30}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({"name":"alex"}) { "_id" : ObjectId("5a97a6dec51b3387784da134"), "name" : "alex", "age" : 30 } 要点:使用$set,如果这个key-value不存在,就创建这个 > db.foo.update({"name":"alex"},{"$set":{"book":"linux shell"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({"name":"alex"}) { "_id" : ObjectId("5a97a6dec51b3387784da134"), "name" : "alex", "age" : 30, "book" : "linux shell" } “$unset”可以将这个键值删除 > db.foo.update({"name":"alex"},{"$unset":{"book":"linux shell"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({"name":"alex"}) { "_id" : ObjectId("5a97a6dec51b3387784da134"), "name" : "alex", "age" : 30 } $inc修饰器是用来增加已有健的值,或者在健不存在的时候,创建一个键 > db.games.insert({"game":"pinball","user":"joe"}) WriteResult({ "nInserted" : 1 }) > db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":50}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.games.find({"user":"joe"}) (发现创建了一个键,并赋值50) { "_id" : ObjectId("5a97b06ec51b3387784da137"), "game" : "pinball", "user" : "joe", "score" : 50 } > db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":100}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.games.find({"user":"joe"}) (如果这个键存在,根据上面设置发现增加了100,变成150咯) { "_id" : ObjectId("5a97b06ec51b3387784da137"), "game" : "pinball", "user" : "joe", "score" : 150 } > db.foo.find() { "_id" : ObjectId("5a97a6d0c51b3387784da133"), "name" : "joe", "age" : 25 } { "_id" : ObjectId("5a97a6dec51b3387784da134"), "name" : "alex", "age" : 30 } { "_id" : ObjectId("5a97a6e2c51b3387784da135"), "name" : "yuki", "age" : 20 } { "_id" : ObjectId("5a97a6e7c51b3387784da136"), "name" : "louis", "age" : 20 } > db.foo.update({"name":"joe"},{"$inc":{"age":10}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({"name":"joe"}) { "_id" : ObjectId("5a97a6d0c51b3387784da133"), "name" : "joe", "age" : 35 } 数组修饰器: 特点:只能用在值为数组的键上 $push对已有数组末尾添加一个元素,要是没有,就会创建一个新的数组 > db.blog.insert({"titile":"a blog post","content":"...."}) WriteResult({ "nInserted" : 1 }) > db.blog.update({"titile":"a blog post"},{$push:{"comments":{"name":"joe","email":"joe15@163.com","content":"nice post"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.find({"titile":"a blog post"}) { "_id" : ObjectId("5a97b727c51b3387784da138"), "titile" : "a blog post", "content" : "....", "comments" : [ { "name" : "joe", "email" : "joe15@163.com", "content" : "nice post" } ] } > db.blog.update({"titile":"a blog post"},{"$push":{"comments":{"name":"yuki","email":"yuki25@163.com","content":"yuki post"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.find({"titile":"a blog post"}) { "_id" : ObjectId("5a97b727c51b3387784da138"), "titile" : "a blog post", "content" : "....", "comments" : [ { "name" : "joe", "email" : "joe15@163.com", "content" : "nice post" }, { "name" : "yuki", "email" : "yuki25@163.com", "content" : "yuki post" } ] } $addToSet 添加新的值,可以避免重复 > db.email.insert({"username":"joe","email" : ["joe@example.com","joe@gmail.com"]}) WriteResult({ "nInserted" : 1 }) > db.email.update({"username":"joe"},{"$addToSet":{"email":"joe@gmail.com"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.email.find({"username":"joe"}) { "_id" : ObjectId("5a97b96fc51b3387784da139"), "username" : "joe", "email" : [ "joe@example.com", "joe@gmail.com" ] } > db.email.update({"username":"joe"},{"$addToSet":{"email":{"joetest@163.com"}}}) 2018-03-01T16:29:25.409+0800 E QUERY [thread1] SyntaxError: missing : after property id @(shell):1:75 > db.email.update({"username":"joe"},{"$addToSet":{"email":"joetest@163.com"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.email.find({"username":"joe"}) { "_id" : ObjectId("5a97b96fc51b3387784da139"), "username" : "joe", "email" : [ "joe@example.com", "joe@gmail.com", "joetest@163.com" ] } 注意:使用$addToSet和$each组合起来,可以添加多个不同的值,如果想一次添加多个邮件地址 > db.email.update({"username":"joe"},{"$addToSet":{"email":{"$each":["test01@163.com","test02@163.com","test03@163.com"]}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.email.find() { "_id" : ObjectId("5a97b96fc51b3387784da139"), "username" : "joe", "email" : [ "joe@example.com", "joe@gmail.com", "joetest@163.com", "test01@163.com", "test02@163.com", "test03@163.com" ] } $pull 根据特定条件来从数组中删除元素 > db.email.update({"username":"joe"},{"$pull":{"email":"test01@163.com"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.email.find() { "_id" : ObjectId("5a97b96fc51b3387784da139"), "username" : "joe", "email" : [ "joe@example.com", "joe@gmail.com", "joetest@163.com", "test02@163.com", "test03@163.com" ] } ~~~ 第三部分:索引 类型: B+ Tree 、hash索引、空间索引、全文索引 MongoDB支持的索引 1)单字段索引----Single 默认所有集合是通过_id进行索引,应用程序和用户可以添加其他的字段来进行索引, 案例: > db.records.insert({"score":1034,"location":{state:"NY",city:"shanghai"}}) WriteResult({ "nInserted" : 1 }) > db.records.find() { "_id" : ObjectId("5a97ceafc1670638c95ceb9c"), "score" : 1034, "location" : { "state" : "NY", "city" : "shanghai" } } 以下操作score 在records集合的字段上创建一个升序索引 值1指定按升序排列项目的索引。值-1指定按降序对项目进行排序的索引 2)组合索引 3)多键索引(Multikey)---(如:key:{}) 4)文本索引(全文索引) 5)hash索引