python之PyMongo使用總結(jié)
PyMongo是什么
PyMongo是驅(qū)動(dòng)程序,使python程序能夠使用Mongodb數(shù)據(jù)庫,使用python編寫而成.
安裝
環(huán)境:Ubuntu 14.04+python2.7+MongoDB 2.4
先去官網(wǎng)下載軟件包,地址點(diǎn)擊打開鏈接.解壓縮后進(jìn)入,使用python setup.py install 進(jìn)行安裝
或者用pip安裝pip -m install pymongo
基本使用
創(chuàng)建連接
import pymongo client = pymongo.MongoClient('localhost', 27017)
或者可以這樣
import pymongo client = MongoClient('mongodb://localhost:27017/')
連接數(shù)據(jù)庫
db = client.mydb #或者 db = client['mydb']
連接聚集
聚集相當(dāng)于關(guān)系型數(shù)據(jù)庫中的表
collection = db.my_collection #或者 collection = db['my_collection']
查看數(shù)據(jù)庫下所有聚集名稱
db.collection_names()
插入記錄
collection.insert({"key1":"value1","key2","value2"})
刪除記錄
全部刪除
collection.remove()
按條件刪除
collection.remove({"key1":"value1"})
更新記錄
collection.update({"key1": "value1"}, {"$set": {"key2": "value2", "key3": "value3"}})
查詢記錄
查詢一條記錄:find_one()不帶任何參數(shù)返回第一條記錄.帶參數(shù)則按條件查找返回
collection.find_one() collection.find_one({"key1":"value1"})
查詢多條記錄:find()不帶參數(shù)返回所有記錄,帶參數(shù)按條件查找返回
collection.find() collection.find({"key1":"value1"})
查看聚集的多條記錄
for item in collection.find(): print item
查看聚集記錄的總數(shù)
print collection.find().count()
查詢結(jié)果排序
單列上排序
collection.find().sort("key1") # 默認(rèn)為升序 collection.find().sort("key1", pymongo.ASCENDING) # 升序 collection.find().sort("key1", pymongo.DESCENDING) # 降序
多列上排序
collection.find().sort([("key1", pymongo.ASCENDING), ("key2", pymongo.DESCENDING)])
實(shí)例1:
#!/usr/bin/env python #coding:utf-8 # Author: --<qingfengkuyu> # Purpose: MongoDB的使用 # Created: 2014/4/14 #32位的版本最多只能存儲(chǔ)2.5GB的數(shù)據(jù)(NoSQLFan:最大文件尺寸為2G,生產(chǎn)環(huán)境推薦64位) import pymongo import datetime import random #創(chuàng)建連接 conn = pymongo.Connection('10.11.1.70',27017) #連接數(shù)據(jù)庫 db = conn.study #db = conn['study'] #打印所有聚集名稱,連接聚集 print u'所有聚集:',db.collection_names() posts = db.post #posts = db['post'] print posts #插入記錄 new_post = {"AccountID":22,"UserName":"libing",'date':datetime.datetime.now()} new_posts = [{"AccountID":22,"UserName":"liuw",'date':datetime.datetime.now()}, {"AccountID":23,"UserName":"urling",'date':datetime.datetime.now()}]#每條記錄插入時(shí)間都不一樣 posts.insert(new_post) #posts.insert(new_posts)#批量插入多條數(shù)據(jù) #刪除記錄 print u'刪除指定記錄:\n',posts.find_one({"AccountID":22,"UserName":"libing"}) posts.remove({"AccountID":22,"UserName":"libing"}) #修改聚集內(nèi)的記錄 posts.update({"UserName":"urling"},{"$set":{'AccountID':random.randint(20,50)}}) #查詢記錄,統(tǒng)計(jì)記錄數(shù)量 print u'記錄總計(jì)為:',posts.count(),posts.find().count() print u'查詢單條記錄:\n',posts.find_one() print posts.find_one({"UserName":"liuw"}) #查詢所有記錄 print u'查詢多條記錄:' #for item in posts.find():#查詢?nèi)坑涗? #for item in posts.find({"UserName":"urling"}):#查詢指定記錄 #for item in posts.find().sort("UserName"):#查詢結(jié)果根據(jù)UserName排序,默認(rèn)為升序 #for item in posts.find().sort("UserName",pymongo.ASCENDING):#查詢結(jié)果根據(jù)UserName排序,ASCENDING為升序,DESCENDING為降序 for item in posts.find().sort([("UserName",pymongo.ASCENDING),('date',pymongo.DESCENDING)]):#查詢結(jié)果根據(jù)多列排序 print item #查看查詢語句的性能 #posts.create_index([("UserName", pymongo.ASCENDING), ("date", pymongo.DESCENDING)])#加索引 print posts.find().sort([("UserName",pymongo.ASCENDING),('date',pymongo.DESCENDING)]).explain()["cursor"]#未加索引用BasicCursor查詢記錄 print posts.find().sort([("UserName",pymongo.ASCENDING),('date',pymongo.DESCENDING)]).explain()["nscanned"]#查詢語句執(zhí)行時(shí)查詢的記錄數(shù)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PHP webshell檢查工具 python實(shí)現(xiàn)代碼
Web安全應(yīng)急響應(yīng)中,不免要檢查下服務(wù)器上是否被上傳了webshell,手工檢查比較慢,就寫了個(gè)腳本來檢查了。Windows平臺(tái)下已經(jīng)有了lake2寫的雷克圖的了,一般的檢查也夠用了,寫了個(gè)Linux下面的,用python寫的。2009-09-09Python基于回溯法子集樹模板實(shí)現(xiàn)8皇后問題
這篇文章主要介紹了Python基于回溯法子集樹模板實(shí)現(xiàn)8皇后問題,簡單說明了8皇后問題的原理并結(jié)合實(shí)例形式分析了Python回溯法子集樹模板解決8皇后問題的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09Python3實(shí)現(xiàn)并發(fā)檢驗(yàn)代理池地址的方法
這篇文章主要介紹了Python3實(shí)現(xiàn)并發(fā)檢驗(yàn)代理池地址的方法,實(shí)例分析了Python3基于線程的代理檢驗(yàn)操作相關(guān)技巧,需要的朋友可以參考下2016-09-09基于數(shù)據(jù)歸一化以及Python實(shí)現(xiàn)方式
今天小編就為大家分享一篇基于數(shù)據(jù)歸一化以及Python實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07python數(shù)據(jù)分析apply(),map(),applymap()用法
這篇文章主要介紹了python數(shù)據(jù)分析apply(),map(),applymap()用法,可以方便地實(shí)現(xiàn)對(duì)批量數(shù)據(jù)的自定義操作。用法歸納如下,需要的朋友可以參考一下2022-03-03Python爬取新型冠狀病毒“謠言”新聞進(jìn)行數(shù)據(jù)分析
這篇文章主要介紹了Python爬取新型冠狀病毒“謠言”新聞進(jìn)行數(shù)據(jù)分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Python中關(guān)鍵字is與==的區(qū)別簡述
這篇文章主要介紹了Python中關(guān)鍵字is與==的區(qū)別,對(duì)于Python初學(xué)者有一定的借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-07-07