十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
使用pip install pymongo安裝
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),洛扎企業(yè)網(wǎng)站建設(shè),洛扎品牌網(wǎng)站建設(shè),網(wǎng)站定制,洛扎網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,洛扎網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
1.連接MongoDB實(shí)例
In [60]: from pymongo import MongoClient In [61]: client=MongoClient('mongodb://10.10.41.25:2911') In [62]: client=MongoClient('10.10.41.25',2911)
兩種寫法都行
2.獲取數(shù)據(jù)庫信息
In [63]: db=client.game In [64]: db=client['game']
兩種寫法都行
3.獲取集合信息
In [85]: collection=db.player In [86]: collection=db['player']
兩種寫法都行
4.插入一個(gè)文檔記錄
MongoDB以JSON格式存儲(chǔ)和顯示數(shù)據(jù)。在pymongo中以字典的方式顯示數(shù)據(jù)。
In [95]: import datetime In [96]: post={"author":"Mike","text":"My first blog post!","tags":["mongodb","python","pymongo"],"date":datetime.datetime.utcnow()}
In [132]: posts=db.posts In [133]: post_id=posts.insert(post) In [134]: post_id Out[134]: ObjectId('550ad8677a50900165feae9d')
當(dāng)插入一個(gè)文檔時(shí),一個(gè)特殊的key,"_id"將自動(dòng)添加到這個(gè)文檔中。
In [136]: db.collection_names() Out[136]: [u'system.indexes',u'posts']
5.使用find_one()獲取單個(gè)文檔
In [141]: posts.find_one() Out[141]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'} In [142]: posts.find_one({"author":"Mike"}) Out[142]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'} In [143]: posts.find_one({"author":"Eliot"}) In [144]:
MongoDB以BSON格式存儲(chǔ)字符,而BSON字符串是以UTF-8編碼,所以PyMongo必須要確保它存儲(chǔ)的數(shù)據(jù)是有效的UTF-8編碼的數(shù)據(jù)。常規(guī)字符串直接存儲(chǔ),但是經(jīng)過編碼的字符串首先以UTF-8編碼存儲(chǔ)。
6.使用ObjectID查找文檔
In [151]: post_id Out[151]: ObjectId('550ad8677a50900165feae9d') In [152]: posts.find_one({"_id":post_id}) Out[152]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'}
ObjectID和它表示的字符串不一樣
In [154]: post_id_as_str=str(post_id) In [155]: posts.find_one({"_id":post_id_as_str})
沒有任何結(jié)果顯示
在一些WEB應(yīng)用中,需要更加URL獲取post_id進(jìn)而根據(jù)post_id查找匹配的文檔。在使用find_one()查找之前有必要將post_id從字符串轉(zhuǎn)換成為ObjectID
7.批量插入文檔數(shù)據(jù)
>>> new_posts = [{"author": "Mike",... "text": "Another post!", "tags": ["bulk", "insert"], "date": datetime.datetime(2009, 11, 12, 11, 14)}, {"author": "Eliot", "title": "MongoDB is fun", "text": "and pretty easy too!", "date": datetime.datetime(2009, 11, 10, 10, 45)}] >>> posts.insert(new_posts)[ObjectId('...'), ObjectId('...')]
8.查詢多個(gè)文檔數(shù)據(jù)
In [165]: for post in posts.find(): post .....: .....: Out[166]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'} Out[166]: {u'_id': ObjectId('550b87d47a50907021e3473b'), u'author': u'Mike', u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!'} Out[166]: {u'_id': ObjectId('550b87d47a50907021e3473c'), u'author': u'Eliot', u'title': u'MongoDB is fun'}
In [169]: for post in posts.find({"author" : "Mike"}): .....: post .....: .....: Out[169]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'} Out[169]: {u'_id': ObjectId('550b87d47a50907021e3473b'), u'author': u'Mike', u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!'}
9.總計(jì)
In [170]: posts.count() Out[170]: 3 In [171]: posts.find({"author":"Mike"}).count() Out[171]: 2
10.范圍查詢
In [183]: d=datetime.datetime(2009,11,12,12) In [184]: for post in posts.find({"date":{"$lt":d}}).sort("author"): .....: print post .....: .....: {u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('550b87d47a50907021e3473b'), u'author': u'Mike'}
11.索引
使用索引可以加快查詢速度,縮小查詢范圍。
In [201]: posts.find({"date" : {"$lt":d}}).sort("author").explain()["cursor"] Out[201]: u'BasicCursor' In [202]: posts.find({"date" : {"$lt":d}}).sort("author").explain()["nscanned"] Out[202]: 3
創(chuàng)建組合索引
In [241]: from pymongo import ASCENDING,DESCENDING In [242]: posts.create_index([("date",DESCENDING),("author",ASCENDING)]) Out[242]: u'date_-1_author_1' In [243]: posts.find({"date" : {"$lt":d}}).sort("author").explain()["nscanned"] Out[243]: 1
12.
參考文檔
http://api.mongodb.org/python/current/tutorial.html?_ga=1.58141740.722641156.1410499072