python之PyMongo使用總結

2021-08-27 03:24:49 字數 1716 閱讀 5535

個人分類: python

pymongo是驅動程式,使python程式能夠使用mongodb資料庫,使用python編寫而成.

環境:ubuntu 14.04+python2.7+mongodb 2.4

進行安裝

或者用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()
全部刪除

collection.remove()
按條件刪除

collection.remove()
collection.update(, })
查詢一條記錄:find_one()不帶任何引數返回第一條記錄.帶引數則按條件查詢返回

collection.find_one()

collection.find_one()

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

collection.find()

collection.find()

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)])

python 模組 pymongo模組

mongodb 資料庫 pymongo 操作 import pymongo 連線mongo資料庫 client pymongo.mongoclient host localhost port 27017 獲取應資料庫 db client.text 獲取資料表 my collection db.col...

python基礎之if,while,for使用方法

條件選擇結構格式 if else a 30if a 100and a 150 注意冒號 print 你好 else 注意冒號 print hello world 輸出hello worldif elif else a 30if a 50and a 150 print 你好 elif a 150 pr...

Python3中PyMongo使用舉例

mongodb是乙個基於分布式檔案儲存的開源資料庫,由c 語言編寫,與平台無關,旨在為web應用提供可擴充套件的高效能資料儲存解決方案。mongodb是乙個介於關聯式資料庫和非關聯式資料庫之間的產品,是非關聯式資料庫中功能最豐富,最像關聯式資料庫的。它支援的資料結構非常鬆散,是類似json的bson...