mongodb pymongo api 簡單測試
MongoDB 是一個基於分佈式文件存儲的數據庫。由 C++ 語言編寫。旨在爲 WEB 應用提供可擴展的高性能數據存儲解決方案。MongoDB 是一個介於關係數據庫和非關係數據庫之間的產品,是非關係數據庫當中功能最豐富,最像關係數據庫的。
不管學習什麼內容,我們都應該從它的基礎開始,掌握它的基礎知識,才能用好它。
import pymongo
import random
TEST_DOC_ONE = {'title': 'MongoDB Sola Test',
'description': 'a nosql database',
'by': 'Sola',
'url': 'blog.csdn.net/qq_17308321',
'tags': ['mongodb', 'database', 'NoSQL'],
'likes': 100
}
TEST_DOC_MANY = [{'title': 'Doc Many - 1',
'description': 'a nosql database',
'by': 'Sola',
'url': 'blog.csdn.net/qq_17308321',
'tags': ['mongodb', 'database', 'NoSQL'],
'likes': 110
},
{'title': 'Doc Many - 2',
'description': 'a nosql database',
'by': 'Sola',
'url': 'blog.csdn.net/qq_17308321',
'tags': ['mongodb', 'database', 'NoSQL'],
'likes': 190
},
{'title': 'Doc Many - 3',
'description': 'a nosql database',
'by': 'Sola',
'url': 'blog.csdn.net/qq_17308321',
'tags': ['mongodb', 'database', 'NoSQL'],
'likes': 160
},
{'title': 'Doc Many - 6',
'description': 'a nosql database',
'by': 'Sola',
'url': 'blog.csdn.net/qq_17308321',
'tags': ['mongodb', 'database', 'NoSQL'],
'likes': 130
},
{'title': 'Doc Many - 5',
'description': 'a nosql database',
'by': 'Sola',
'url': 'blog.csdn.net/qq_17308321',
'tags': ['mongodb', 'database', 'NoSQL'],
'likes': 10
},
{'title': 'Doc Many - 4',
'description': 'a nosql database',
'by': 'Sola',
'url': 'blog.csdn.net/qq_17308321',
'tags': ['mongodb', 'database', 'NoSQL'],
'likes': 60
},
]
mongos = {
"host": ["172.16.0.50:34001"],
"replicaSet": "rs0",
"readPreference": "secondaryPreferred",
}
DB_NAME = 'testdb'
COL_NAME = 'testcol'
INDE_NAME = 'testindex'
MONGOCONN = pymongo.MongoClient(**mongos)
MONGODB = MONGOCONN[DB_NAME]
# simple test
# delete cllection
MONGODB.drop_collection(COL_NAME)
# create collections
if COL_NAME not in MONGODB.list_collection_names(include_system_collections=False):
col = MONGODB.create_collection(COL_NAME)
# clear
res = col.delete_many({})
print("clear res :", res)
# insert documents
# insert one
res = col.insert_one(TEST_DOC_ONE)
print("ineset one res :", res)
# insert many
res = col.insert_many(TEST_DOC_MANY)
print("insert many res :", res)
# count
print("count : ", col.count_documents({}))
# serach one
doc = col.find_one({'title':'Doc Many - 3'})
print(doc)
# update one : Doc Many - 3
res = col.update_one({'_id':doc['_id']}, {"$set": {'likes': 150}})
print("update count : ", res.modified_count)
# serach all
print("----------all-------------")
for doc in col.find({}):
print(doc)
print("----------sor-------------")
# cursor sort by like
for doc in col.find().sort([('likes', pymongo.ASCENDING)]):
print(doc)
""" Terminal output
clear res : <pymongo.results.DeleteResult object at 0x7f27ef037eb0>
ineset one res : <pymongo.results.InsertOneResult object at 0x7f27ef031dc0>
insert many res : <pymongo.results.InsertManyResult object at 0x7f27ef045d70>
count : 7
{'_id': ObjectId('5eaa8f519c4073242a2c4d14'), 'title': 'Doc Many - 3', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 160}
update count : 1
----------all-------------
{'_id': ObjectId('5eaa8f519c4073242a2c4d11'), 'title': 'MongoDB Sola Test', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 100}
{'_id': ObjectId('5eaa8f519c4073242a2c4d12'), 'title': 'Doc Many - 1', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 110}
{'_id': ObjectId('5eaa8f519c4073242a2c4d13'), 'title': 'Doc Many - 2', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 190}
{'_id': ObjectId('5eaa8f519c4073242a2c4d14'), 'title': 'Doc Many - 3', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 150}
{'_id': ObjectId('5eaa8f519c4073242a2c4d15'), 'title': 'Doc Many - 6', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 130}
{'_id': ObjectId('5eaa8f519c4073242a2c4d16'), 'title': 'Doc Many - 5', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 10}
{'_id': ObjectId('5eaa8f519c4073242a2c4d17'), 'title': 'Doc Many - 4', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 60}
----------sor-------------
{'_id': ObjectId('5eaa8f519c4073242a2c4d16'), 'title': 'Doc Many - 5', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 10}
{'_id': ObjectId('5eaa8f519c4073242a2c4d17'), 'title': 'Doc Many - 4', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 60}
{'_id': ObjectId('5eaa8f519c4073242a2c4d11'), 'title': 'MongoDB Sola Test', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 100}
{'_id': ObjectId('5eaa8f519c4073242a2c4d12'), 'title': 'Doc Many - 1', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 110}
{'_id': ObjectId('5eaa8f519c4073242a2c4d15'), 'title': 'Doc Many - 6', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 130}
{'_id': ObjectId('5eaa8f519c4073242a2c4d14'), 'title': 'Doc Many - 3', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 150}
{'_id': ObjectId('5eaa8f519c4073242a2c4d13'), 'title': 'Doc Many - 2', 'description': 'a nosql database', 'by': 'Sola', 'url': 'blog.csdn.net/qq_17308321', 'tags': ['mongodb', 'database', 'NoSQL'], 'likes': 190}
"""
print("----------ind-------------")
DELETE_NOW_COL = 'off'
INDEX_MODE_SWITCH = 'off'
# index test
# delete collection
if DELETE_NOW_COL == 'on':
MONGODB.drop_collection(INDE_NAME)
# create collections and insert 30w data
if INDE_NAME not in MONGODB.list_collection_names(include_system_collections=False):
col = MONGODB.create_collection(INDE_NAME)
# insert 30w data
i = 1
while i <= 300000:
staff={
'jobnumber':"staff{:08d}".format(i),
'age': random.randint(20, 50),
'gender': 'boy' if random.randint(0, 1) else 'girl'
}
col.insert_one(staff)
i = i + 1
print("insert count : ", col.count_documents({}))
col = MONGODB[INDE_NAME]
col.drop_indexes()
print("3 example data")
for doc in col.find().limit(3):
print(doc)
print("----------nic-------------")
# no index cost
exp = col.find({'jobnumber': 'staff00210004'}).explain()
#doc = col.find({'jobnumber': 'staff00280666'})
#print(doc, doc.explain())
print(f"no index cost:{exp['executionStats']['executionTimeMillis']} ms")
# index cost
print("----------inc-------------")
index_asce = pymongo.IndexModel([('jobnumber', pymongo.ASCENDING)])
col.create_indexes([index_asce])
exp = col.find({'jobnumber': 'staff00210004'}).explain()
print(f"asce index cost:{exp['executionStats']['executionTimeMillis']} ms")
# drop all index
print("----------dic-------------")
col.drop_indexes()
exp = col.find({'jobnumber': 'staff00210004'}).explain()
print(f"after drop index cost:{exp['executionStats']['executionTimeMillis']} ms")
# hash index
print("----------hic-------------")
index_hash = pymongo.IndexModel([('jobnumber', pymongo.HASHED)])
col.create_indexes([index_hash])
exp = col.find({'jobnumber': 'staff00210004'}).explain()
print(f"hash index cost:{exp['executionStats']['executionTimeMillis']} ms")
col.drop_indexes()
# Compound index
print("----------cic-------------")
exp = col.find({'age': '28'}, {'gender':'girl'}).explain()
print(f"no Compound index cost:{exp['executionStats']['executionTimeMillis']} ms")
index_comp = pymongo.IndexModel([('gender', pymongo.ASCENDING), ('age', pymongo.ASCENDING)], background=True)
col.create_indexes([index_comp])
exp = col.find({'gender':'girl', 'age': '28'}).explain()
print(f"ha Compound index cost:{exp['executionStats']['executionTimeMillis']} ms")
col.drop_indexes()
""" Terminal output
----------ind-------------
3 example data
{'_id': ObjectId('5eaa9cf545463e7ebf2791f5'), 'jobnumber': 'staff00000001', 'age': 22, 'gender': 'girl'}
{'_id': ObjectId('5eaa9cf545463e7ebf2791f6'), 'jobnumber': 'staff00000002', 'age': 47, 'gender': 'boy'}
{'_id': ObjectId('5eaa9cf545463e7ebf2791f7'), 'jobnumber': 'staff00000003', 'age': 42, 'gender': 'boy'}
----------nic-------------
no index cost:201 ms
----------inc-------------
asce index cost:1 ms
----------dic-------------
after drop index cost:200 ms
----------hic-------------
hash index cost:1 ms
----------cic-------------
no Compound index cost:200 ms
ha Compound index cost:1 ms
"""