Chapter2: MongoDB
This section contain the following items:
1.What is MongoDB?
2.build MongoDB environment on Ubuntu (14.04)
3.MongoDB debug message
4.Backup and Restore
5.Use mongoDb by using python
6.MongoDB的基本操作
1.What is MongoDB?
2.build MongoDB environment on Ubuntu (14.04)
1)echo “deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee /etc/apt/sources.list.d/mongo.list:
建立/etc/apt/sources.list.d/mongo.list,並寫入deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen(作為操作的一部分,APT使用一個檔案列出可獲得套裝軟體的鏡像站台位址件- /etc/apt/sources.list.d
檔案中的各項資訊通常按如下格式列出:deb http://host/debian distribution section1 section2 section3)
2)sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10:
如果公鑰已經上傳到Key Server,可以使用下列指令下載公鑰:
gpg --keyserver hkp://wwwkeys.us.pgp.net --recv-keys <金鑰指紋>
3)sudo apt-get update:
進行伺服器與用戶端的套件表頭清單更新, 在 apt-get update 後,再使用 apt-get dist-upgrade 這樣就能夠將整個系統升級
4)sudo apt-get install mongodb-10gen
5)ps -ef | grep mongo:
確定 mongodb已在執行
6)ls -ls /usr/bin | grep mongo
7)ls -ls /etc/init.d | grep mongo
8)sudo service mogodb start, sudo service mogodb stop, sudo service mogodb restart3.MongoDB debug message
4.Backup and Restore
5.Use mongoDb by using python
6.MongoDB的基本操作
與SQL的名詞對照
與SQL指令的對照表
MongoDB
My SQL
新增
db.collection.find(條件)
select
修改
db.collection.insert(document)
insert
刪除
db.collection.update( criteria, objNew, upsert, multi )
update
查詢
db.collection.remove()
delete
新增
修改
刪除
查詢
find
db.collection.find(query, projection)
定義: 從一個collection中找出documents,並回傳一個cursor物件
query:
Query Selectors:
2.Update Operators:
3.Aggregation Pipeline Operators:
https://docs.mongodb.com/v3.2/reference/operator/aggregation/
4.Query Modifiers:
https://docs.mongodb.com/v3.2/reference/operator/query-modifier/
projection:
projection決定了哪些欄位要被回傳
cursor:
一個指向a set of query的指標, 使用者可用迭代的方式從cursor取回查詢結果. 預設timeout為10分鐘
取回方式
1.手動迭代
2.轉為array
example:
取出投過票的人
findOne
Last updated
Was this helpful?