国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python之PyMongo使用總結

2020-01-04 17:14:49
字體:
來源:轉載
供稿:網友

 PyMongo是什么

PyMongo是驅動程序,使python程序能夠使用Mongodb數據庫,使用python編寫而成.

安裝

環境:Ubuntu 14.04+python2.7+MongoDB 2.4

先去官網下載軟件包,地址點擊打開鏈接.解壓縮后進入,使用python setup.py install 進行安裝

或者用pip安裝pip -m install pymongo

基本使用

創建連接

import pymongo client = pymongo.MongoClient('localhost', 27017) 

或者可以這樣

import pymongo client = MongoClient('mongodb://localhost:27017/') 

連接數據庫

db = client.mydb #或者db = client['mydb'] 

連接聚集

聚集相當于關系型數據庫中的表

collection = db.my_collection #或者collection = db['my_collection'] 

查看數據庫下所有聚集名稱

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()不帶任何參數返回第一條記錄.帶參數則按條件查找返回

collection.find_one() collection.find_one({"key1":"value1"}) 

查詢多條記錄:find()不帶參數返回所有記錄,帶參數按條件查找返回

collection.find() collection.find({"key1":"value1"}) 

查看聚集的多條記錄

for item in collection.find():     print item 

查看聚集記錄的總數

print collection.find().count() 

查詢結果排序

單列上排序

collection.find().sort("key1") # 默認為升序 collection.find().sort("key1", pymongo.ASCENDING) # 升序 collection.find().sort("key1", pymongo.DESCENDING) # 降序 

多列上排序

 

復制代碼 代碼如下:

collection.find().sort([("key1", pymongo.ASCENDING), ("key2", pymongo.DESCENDING)]) 

 

實例1:

#!/usr/bin/env python#coding:utf-8# Author:  --<qingfengkuyu># Purpose: MongoDB的使用# Created: 2014/4/14#32位的版本最多只能存儲2.5GB的數據(NoSQLFan:最大文件尺寸為2G,生產環境推薦64位) import pymongoimport datetimeimport random #創建連接conn = pymongo.Connection('10.11.1.70',27017)#連接數據庫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()}]#每條記錄插入時間都不一樣 posts.insert(new_post)#posts.insert(new_posts)#批量插入多條數據 #刪除記錄print u'刪除指定記錄:/n',posts.find_one({"AccountID":22,"UserName":"libing"})posts.remove({"AccountID":22,"UserName":"libing"}) #修改聚集內的記錄posts.update({"UserName":"urling"},{"$set":{'AccountID':random.randint(20,50)}}) #查詢記錄,統計記錄數量print u'記錄總計為:',posts.count(),posts.find().count()print u'查詢單條記錄:/n',posts.find_one()print posts.find_one({"UserName":"liuw"}) #查詢所有記錄print u'查詢多條記錄:'#for item in posts.find():#查詢全部記錄#for item in posts.find({"UserName":"urling"}):#查詢指定記錄#for item in posts.find().sort("UserName"):#查詢結果根據UserName排序,默認為升序#for item in posts.find().sort("UserName",pymongo.ASCENDING):#查詢結果根據UserName排序,ASCENDING為升序,DESCENDING為降序for item in posts.find().sort([("UserName",pymongo.ASCENDING),('date',pymongo.DESCENDING)]):#查詢結果根據多列排序  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"]#查詢語句執行時查詢的記錄數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 固安县| 寿阳县| 德江县| 普安县| 彩票| 南澳县| 凉城县| 新兴县| 文成县| 和平区| 温宿县| 灵武市| 安阳市| 遂平县| 休宁县| 旌德县| 旬阳县| 沁水县| 云和县| 马山县| 黔东| 开平市| 上饶县| 中卫市| 德庆县| 漳平市| 永年县| 石泉县| 瓦房店市| 凯里市| 桐乡市| 上虞市| 工布江达县| 玉山县| 南木林县| 陆丰市| 乐业县| 寿光市| 乐安县| 哈巴河县| 岗巴县|