mongodb 索引:
1,mongodb支持索引,以提升查詢速度
2,mongodb 是介於關系型與非關系型數據庫之間的數據庫
3,可以索引文檔中的任何字段
# 向 t1 數據庫插入 10萬條數據 大約需要 10秒
for(i=0;i<100000;i++){db.t1.insert({name:'test'+i,age:i})}
# 查找 第 10000 條數據 explain() 可以開啟性能測試 executionTimeMillis為 39 毫秒
db.t1.find({name:'test10000'}).explain('executionStats')
# 為集合 name字段 建立索引
db.t1.ensureIndex({name:1})
# 建立索引之后執行時間是 0 毫秒
注意: 查詢 age 字段的話 還是 39 毫秒 因為此字段沒有設立索引
db.t1.find({name:'test10000'}).explain('executionStats')
#查看文檔所有索引
db.t1.getIndexes()
# 刪除 聯合索引
db.t1.dropIndex({"name":1,"age":1})
# 建立唯一索引
db.t1.ensureIndex({"name":1},{"unique":true})
# 建立聯合索引
db.t1.ensureIndex({name:1,age:1})
# 對於聯合索引的查詢 還是按照 聯合索引的順序進行查找 前后一樣 不能只查詢一個
db.t1.find({name:'test10000'},{age:100000}).explain('executionStats')
Mongodb 與python 交互需要安裝 pymongo 包
對於 pymongo 包之中的對象介紹:
MongoClient對象:
client = MongoClient('主機ip',端口)
Database對象:
# client對象獲取獲得數據庫對象
db = client.數據庫名稱
Collections對象:
# 通過db對象獲取集合對象
collections = db.集合名稱
集合操作方法:
'''
insert_one:加入一條文檔對象
insert_many:加入多條文檔對象
find_one:查找一條文檔對象
find:查找多條文檔對象
update_one:更新一條文檔對象
update_many:更新多條文檔對象
delete_one:刪除一條文檔對象
delete_many:刪除多條文檔對象
'''
