一、操作數據的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表示展示

查詢條件:
* $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索引