前言
NoSQL 非關系數據庫。
MongoDB is an open-source, document database designed for ease of development and scaling.
MongoDB是開源的文檔數據庫。文檔也就是關系數據庫里面的一個Record,而文檔的組織形式是collection.
網上找了一些資料都太舊了,還是直接英文文檔最快。 首先官網文檔鎮樓,開頭查了一些中文的MongoDB的資料之類的,發現其實官方文檔最好。
首先介紹一下document,感覺就是Json格式的數據。這里介紹了一下如何導入數據。
{
"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
一個數據就是一個record,然后records由collections組織,然后collections組成了database.
關于安裝
然后安裝省略...
-
然后我們打開安裝后的文件,有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
開啟數據庫
數據庫我們當然要存數據了,首先要指定數據的存儲目錄,當然我們要選個大一點的硬盤啦。
使用以下指令,這里默認安裝在C盤 d:\test\mongodb\data這個目錄指代數據庫數據的目錄。
C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data
如果你的目錄有空格,那么目錄就要雙引了。
C:\mongodb\bin\mongod.exe --dbpath "d:\test\mongo db data"
出現如圖所示界面,那就說明你成功地開啟了數據庫
python作為客戶端操作數據庫
這里介紹了怎么連接數據庫的知識。
- 安裝 pymongo
pip install pymongo
- 導入pymongo模塊
from pymongo import MongoClient
- 創建連接,還有更詳細的配置。
client = MongoClient()
- 使用數據庫
db = client.primer
# 或者
db = client['primer']
- 使用Collection
coll = db.dataset
coll = db['dataset']
- 插入document
coll.insert_one({"key":"value"})
可以插入數組
>>> 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
- 查找數據
cursor = coll.find()
for document in cursor:
print document
如果只是想看一下數據類型
print coll.find_one()
如果查找feild為特定的value的
cursor = db.restaurants.find({"address.zipcode": "10075"})
- 有條件地查找數據
我們之前說的數據是類似鍵值對的組合,
{ <field1>: <value1>, <field2>: <value2>, ... }
然后我們想要 查找鍵為"borough"的值為"Manhattan"的文檔數據.
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}})
同時滿足And
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"})
Or
cursor = db.restaurants.find(
{"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})
- 更新數據
下面的代碼,查找第一個文檔有鍵值對 {"name": "Juni"}的,然后$set用來更新cuisine的值以及 $currentDate來更新lastModified的值。如果要更新的鍵值對不存在,那么會創造新的鍵值對。
result = db.restaurants.update_one(
{"name": "Juni"},
{
"$set": {
"cuisine": "American (New)"
},
"$currentDate": {"lastModified": True}
}
)
- 刪除數據
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> ), ... ]
單領域索引
import pymongo
db.restaurants.create_index([("cuisine", pymongo.ASCENDING)])
混合索引
import pymongo
db.restaurants.create_index([
("cuisine", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])