前言
NoSQL 非關(guān)系數(shù)據(jù)庫酝掩。
MongoDB is an open-source, document database designed for ease of development and scaling.
MongoDB是開源的文檔數(shù)據(jù)庫冗尤。文檔也就是關(guān)系數(shù)據(jù)庫里面的一個(gè)Record,而文檔的組織形式是collection.
網(wǎng)上找了一些資料都太舊了迂尝,還是直接英文文檔最快由驹。 首先官網(wǎng)文檔鎮(zhèn)樓理肺,開頭查了一些中文的MongoDB的資料之類的壳炎,發(fā)現(xiàn)其實(shí)官方文檔最好芽淡。
首先介紹一下document,感覺就是Json格式的數(shù)據(jù)茶凳。這里介紹了一下如何導(dǎo)入數(shù)據(jù)。
{
"address": {
"building": "1007",
"coord": [ -73.856077, 40.848447 ],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
{ "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
{ "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
{ "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
{ "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
],
"name": "Morris Park Bake Shop",
"restaurant_id": "30075445"
}
records(documents),collections,databases
一個(gè)數(shù)據(jù)就是一個(gè)record,然后records由collections組織令境,然后collections組成了database.
關(guān)于安裝
首先下載MongoDB杠园,下載地址,我的是windows系統(tǒng)舔庶,其實(shí)蠻簡單的抛蚁,下載,然后安裝惕橙,因?yàn)楝F(xiàn)在有msi的文件了瞧甩。
然后安裝省略...
-
然后我們打開安裝后的文件,有bin目錄弥鹦,介紹一下這些exe們
bin目錄下
親愛的README給了介紹吧
COMPONENTS
bin/mongod - The database process.
bin/mongos - Sharding controller.
bin/mongo - The database shell (uses interactive javascript).
UTILITIES
bin/mongodump - MongoDB dump tool - for backups, snapshots, etc..
bin/mongorestore - MongoDB restore a dump
bin/mongoexport - Export a single collection to test (JSON, CSV)
bin/mongoimport - Import from JSON or CSV
bin/mongofiles - Utility for putting and getting files from MongoDB GridFS
bin/mongostat - Show performance statistics
開啟數(shù)據(jù)庫
數(shù)據(jù)庫我們當(dāng)然要存數(shù)據(jù)了肚逸,首先要指定數(shù)據(jù)的存儲目錄,當(dāng)然我們要選個(gè)大一點(diǎn)的硬盤啦。
使用以下指令朦促,這里默認(rèn)安裝在C盤 d:\test\mongodb\data這個(gè)目錄指代數(shù)據(jù)庫數(shù)據(jù)的目錄犬钢。
C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data
如果你的目錄有空格,那么目錄就要雙引了思灰。
C:\mongodb\bin\mongod.exe --dbpath "d:\test\mongo db data"
出現(xiàn)如圖所示界面,那就說明你成功地開啟了數(shù)據(jù)庫
python作為客戶端操作數(shù)據(jù)庫
這里介紹了怎么連接數(shù)據(jù)庫的知識混滔。
- 安裝 pymongo
pip install pymongo
- 導(dǎo)入pymongo模塊
from pymongo import MongoClient
- 創(chuàng)建連接洒疚,還有更詳細(xì)的配置。
client = MongoClient()
- 使用數(shù)據(jù)庫
db = client.primer
# 或者
db = client['primer']
- 使用Collection
coll = db.dataset
coll = db['dataset']
- 插入document
coll.insert_one({"key":"value"})
可以插入數(shù)組
>>> db.test.count()
0
>>> result = db.test.insert_many([{'x': i} for i in range(2)])
>>> result.inserted_ids
[ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')]
>>> db.test.count()
2
- 查找數(shù)據(jù)
cursor = coll.find()
for document in cursor:
print document
如果只是想看一下數(shù)據(jù)類型
print coll.find_one()
如果查找feild為特定的value的
cursor = db.restaurants.find({"address.zipcode": "10075"})
- 有條件地查找數(shù)據(jù)
我們之前說的數(shù)據(jù)是類似鍵值對的組合坯屿,
{ <field1>: <value1>, <field2>: <value2>, ... }
然后我們想要 查找鍵為"borough"的值為"Manhattan"的文檔數(shù)據(jù).
cursor = db.restaurants.find({"borough": "Manhattan"})
cursor = db.restaurants.find({"address.zipcode": "10075"})
Greater Than 和Less Than
cursor = db.restaurants.find({"grades.score": {"$gt": 30}})
cursor = db.restaurants.find({"grades.score": {"$lt": 10}})
同時(shí)滿足And
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"})
Or
cursor = db.restaurants.find(
{"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})
- 更新數(shù)據(jù)
下面的代碼油湖,查找第一個(gè)文檔有鍵值對 {"name": "Juni"}的,然后$set用來更新cuisine的值以及 $currentDate來更新lastModified的值领跛。如果要更新的鍵值對不存在乏德,那么會創(chuàng)造新的鍵值對。
result = db.restaurants.update_one(
{"name": "Juni"},
{
"$set": {
"cuisine": "American (New)"
},
"$currentDate": {"lastModified": True}
}
)
- 刪除數(shù)據(jù)
python
刪除符合條件的document
result = db.restaurants.delete_many({"borough": "Manhattan"})
result.deleted_count
刪除全部documents
result = db.restaurants.delete_many({})
result.deleted_count
刪除 collection
db.restaurants.drop()
- 索引
格式
[ ( <field1>: <type1> ), ... ]
單領(lǐng)域索引
import pymongo
db.restaurants.create_index([("cuisine", pymongo.ASCENDING)])
混合索引
import pymongo
db.restaurants.create_index([
("cuisine", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])