1.MongoDB安裝
通過(guò)Homebrew 安裝MongoDB (Mac OS下)
$ brew install mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.2.8.el_capitan.bo
Already downloaded: /Users/lianzhu/Library/Caches/Homebrew/mongodb-3.2.8.el_capitan.bottle.tar.gz
==> Pouring mongodb-3.2.8.el_capitan.bottle.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
brew services start mongodb
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
==> Summary
?? /usr/local/Cellar/mongodb/3.2.8: 17 files, 264.1M
安裝步驟簡(jiǎn)單,關(guān)鍵在mongodb的啟動(dòng).
需要明白brew
的Caveats
說(shuō)的什么意思
To have launchd start mongodb now and restart at login:
brew services start mongodb
//現(xiàn)在和開機(jī)自啟動(dòng)mongo的話使用命令: brew services start mongoldb
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
//不在后臺(tái)啟動(dòng)mongo服務(wù)器使用:mongod --config /usr/local/etc/mongod.conf
怎么關(guān)閉自啟動(dòng)服務(wù):
brew services stop mongodb
mongo的配置文件路徑
/usr/local/etc/mongod.conf
// 2.0的在data/db
// 3.0的dbPath: /usr/local/var/mongodb
PS:由于我在學(xué)習(xí)Python.使用的pymongo所以這里MongoDB的相關(guān)使用沒有寫
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused . 連接被拒一般都是服務(wù)器沒打開
'非正常關(guān)閉,有時(shí)會(huì)造成無(wú)法再次啟動(dòng)數(shù)據(jù)庫(kù),此時(shí)可在配置文件目錄下把.lock的文件刪除即可'
參考:
Homebrew小介紹
Homebrew-github
linux的守護(hù)進(jìn)程與服務(wù)-概念
Mac OS啟動(dòng)服務(wù)優(yōu)化高級(jí)篇
在Mac上安裝MongoDB
2.數(shù)據(jù)庫(kù)修改鍵值
對(duì)于文檔的更新除替換外,針對(duì)某個(gè)或多個(gè)文檔只需要部分更新可使用原子的更新修改器操刀,能夠高效的進(jìn)行文檔更新帝牡。更新修改器是中特殊的鍵
用來(lái)指定復(fù)雜的操作蒂阱,比如增加、刪除或者調(diào)整鍵狂塘,還可能是操作數(shù)組或者內(nèi)嵌文檔
2.1.$inc
示例文檔:{"uid":"201203","type":"1",size:10}
> db.b.insert({"uid":"201203","type":"1",size:10})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 10 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 11 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 2}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 13 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : -1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 12 }
得出結(jié)論:修改器$inc可以對(duì)文檔的某個(gè)值為數(shù)字型(只能為滿足要求的數(shù)字)的鍵進(jìn)行增減的操作录煤。
2.2.$set
用來(lái)指定一個(gè)鍵并更新鍵值,若鍵不存在并創(chuàng)建荞胡。來(lái)看看下面的效果
> db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--size鍵不存在的場(chǎng)合
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"size":10}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--sname鍵存在的場(chǎng)合
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":"ssk"}})
> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "ssk", "type" : "3", "uid" : "20120002" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num"
: 50, "sname" : "jk", "type" : "1", "uid" : "20120002" }
--可改變鍵的值類型
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":["Java",".net","c++"]}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"num" : 40,
"size" : 10,
"sname" : [
"java",
".net",
"c++"
],
"type" : "3",
"uid" : "20120002"
}
對(duì)于內(nèi)嵌的文檔妈踊,$set又是如何進(jìn)行更新的內(nèi)嵌的文檔的呢,請(qǐng)看下面的示例:
示例文檔:{"name":"toyota","type":"suv","size":{"height":10,"width":5,"length":15}}
> db.c.findOne({"name":"toyota"})
{
"_id" : ObjectId("5003be465af21ff428dafbe7"),
"name" : "toyota",
"type" : "suv",
"size" : {
"height" : 10,
"width" : 5,
"length" : 15
}
}
> db.c.update({"name":"toyota"},{"$set":{"size.height":8}})
> db.c.findOne({"name":"toyota"})
{
"_id" : ObjectId("5003be465af21ff428dafbe7"),
"name" : "toyota",
"type" : "suv",
"size" : {
"height" : 8,
"width" : 5,
"length" : 15
}
}
> db.c.update({"name":"toyota"},{"$set":{"size.width":7}})
> db.c.findOne({"name":"toyota"})
{
"_id" : ObjectId("5003be465af21ff428dafbe7"),
"name" : "toyota",
"type" : "suv",
"size" : {
"height" : 8,
"width" : 7,
"length" : 15
}
}
可見:對(duì)于內(nèi)嵌文檔在使用$set更新時(shí)泪漂,使用"."連接的方式廊营。
2.3.$unset
從字面就可以看出其意義,主要是用來(lái)刪除鍵萝勤。
示例操作效果如下:
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"sname":1}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"num" : 40,
"size" : 10,
"type" : "3",
"uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"num":0}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"size" : 10,
"type" : "3",
"uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"size":-1}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"type" : "3",
"uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"desc":"sssssss"}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"type" : "3",
"uid" : "20120002"
}
得出結(jié)論:使用修改器$unset時(shí)露筒,不論對(duì)目標(biāo)鍵使用1、0敌卓、-1或者具體的字符串等都是可以刪除該目標(biāo)鍵慎式。
2.4.數(shù)組修改器--$push
示例操作效果如下:
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv",
"size" : { "height" : 8, "width" : 7, "length" : 15 } }
--先push一個(gè)當(dāng)前文檔中不存在的鍵title
> db.c.update({"name" : "toyota"},{$push:{"title":"t1"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1" ], "type" : "suv" }
再向title中push一個(gè)值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2" ], "type" : "suv" }
--再向title中push一個(gè)值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }
--再向一個(gè)已經(jīng)存在的鍵值非數(shù)組類型的鍵push一個(gè)值
> db.c.update({"name" : "toyota"},{$push:{"size.height":10}})
Cannot apply $push/$pushAll modifier to non-array
> db.c.update({"name" : "toyota"},{$push:{"name":"ddddddd"}})
Cannot apply $push/$pushAll modifier to non-array
得出結(jié)論:$push--向文檔的某個(gè)數(shù)組類型的鍵添加一個(gè)數(shù)組元素,不過(guò)濾重復(fù)的數(shù)據(jù)趟径。添加時(shí)鍵存在瞬捕,要求鍵值類型必須是數(shù)組;鍵不存在舵抹,則創(chuàng)建數(shù)組類型的鍵肪虎。
2.5.數(shù)組修改器--addToSet
主要給數(shù)組類型鍵值添加一個(gè)元素時(shí),避免在數(shù)組中產(chǎn)生重復(fù)數(shù)據(jù)惧蛹,$ne在有些情況是不通行的扇救。
> db.c.update({"title" : {$ne:"t2"}},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }
> db.c.update({"name" : "toyota"},{$addToSet:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }
2.6.數(shù)組修改器--pull
$pop從數(shù)組的頭或者尾刪除數(shù)組中的元素香嗓,示例如下:
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3", "t4" ],"type" : "suv" }
從數(shù)組的尾部刪除 1
> db.c.update({"name" : "toyota"},{$pop:{"title":1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3" ], "type" : "suv" }
從數(shù)組的頭部 -1
> db.c.update({"name" : "toyota"},{$pop:{"title":-1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t2", "t3" ], "type" : "suv" }
從數(shù)組的尾部刪除 0
> db.c.update({"name" : "toyota"},{$pop:{"title":0}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t2" ], "type" : "suv" }
$pull從數(shù)組中刪除滿足條件的元素迅腔,示例如下:
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2", "t3" ],"type" : "suv" }
> db.c.update({"name" : "toyota"},{$pull:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t3" ], "type" : "suv" }
2.7.數(shù)組的定位修改器
在需要對(duì)數(shù)組中的值進(jìn)行操作的時(shí)候,可通過(guò)位置或者定位操作符("$").數(shù)組是0開始的靠娱,可以直接將下標(biāo)作為鍵來(lái)選擇元素沧烈。
示例如下:
{"uid":"001",comments:[{"name":"t1","size":10},{"name":"t2","size":12}]}
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 10 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"uid":"001"},{$inc:{"comments.0.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 11 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"comments.name":"t1"},{$set:{"comments.$.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 1 }, { "name" : "t2", "size" : 12 } ] }
--若為多個(gè)文檔滿足條件,則只更新第一個(gè)文檔像云。
2.8.upsert
upsert是一種特殊的更新锌雀。當(dāng)沒有符合條件的文檔蚂夕,就以這個(gè)條件和更新文檔為基礎(chǔ)創(chuàng)建一個(gè)新的文檔,如果找到匹配的文檔就正常的更新腋逆。
使用upsert锣吼,既可以避免競(jìng)態(tài)問(wèn)題蒋畜,也可以減少代碼量(update的第三個(gè)參數(shù)就表示這個(gè)upsert环鲤,參數(shù)為true時(shí))
> db.c.remove()
> db.c.update({"size":11},{$inc:{"size":3}})
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},false)
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},true)
> db.c.find()
{ "_id" : ObjectId("5003ded6c28f67507a6df1de"), "size" : 14 }
2.9.save函數(shù)
1.可以在文檔不存在的時(shí)候插入裁奇,存在的時(shí)候更新,只有一個(gè)參數(shù)文檔撑蚌。
2.要是文檔含有"_id"上遥,會(huì)調(diào)用upsert。否則争涌,會(huì)調(diào)用插入粉楚。
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 50,
"sname" : "jk", "type" : "1", "uid" : "20120002" }
> var o = db.a.findOne()
> o.num = 55
55
> db.a.save(o)
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 55,
"sname" : "jk", "type" : "1", "uid" : "20120002" }
原文出處留下方便查詢