python之PyMongo使用總結(jié)
PyMongo是什么
PyMongo是驅(qū)動程序,使python程序能夠使用Mongodb數(shù)據(jù)庫,使用python編寫而成.
安裝
環(huán)境:Ubuntu 14.04+python2.7+MongoDB 2.4
先去官網(wǎng)下載軟件包,地址點擊打開鏈接.解壓縮后進入,使用python setup.py install 進行安裝
或者用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']
連接聚集
聚集相當于關(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") # 默認為升序
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的數(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()}]#每條記錄插入時間都不一樣
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)計記錄數(shù)量
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():#查詢?nèi)坑涗?
#for item in posts.find({"UserName":"urling"}):#查詢指定記錄
#for item in posts.find().sort("UserName"):#查詢結(jié)果根據(jù)UserName排序,默認為升序
#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ù)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Win10操作系統(tǒng)中PyTorch虛擬環(huán)境配置+PyCharm配置
本文主要介紹了Win10操作系統(tǒng)中PyTorch虛擬環(huán)境配置+PyCharm配置,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
python如何根據(jù)x軸、y軸坐標在坐標軸里畫出曲線圖
這篇文章主要介紹了python如何根據(jù)x軸、y軸坐標在坐標軸里畫出曲線圖問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

