閱讀目錄
回到頂部
pymongo是操作mongodb的python模組
回到頂部
# easy_install pymongo回到頂部
>>>import pymongo回到頂部>>> conn = pymongo.mongoclient(host=host,port=port,tz_aware=false)
>>>conn.database_names()[u'回到頂部test1
', u'
test2
', u'
admin
', u'
local
']
>>> db = conn.get_database("回到頂部db_name
")
>>> db.authenticate('回到頂部username
', '
password
')true
>>>db.collection_names()[u'回到頂部account
', u'
role
', u'
item
', u'
online
']
>>> account = db.get_collection("回到頂部col_name
")
>>> account.find_one()回到頂部
>>> account.find_one().keys()回到頂部
>>> for i in account.find回到頂部():... print i
>>> account.find().count()回到頂部
>>> for i in account.find():回到頂部... print i
>>> account.find().sort("回到頂部name
", pymongo.ascending)
>>> account.find().sort([("
name
", pymongo.ascending), ("
active_time
", pymongo.descending)])
>>> account.insert()回到頂部
>>> account.update(, })回到頂部注:如果資料中沒有鍵-值"
name
": "
mike
", 會新增"
active_time
": "
20130408120000
"
>>> account.remove()回到頂部
方法1.回到頂部import re
方法2.
account.find( }, } ] })回到頂部
#! /usr/bin/envpython
# --*-- coding:utf-8 --*--import pymongo
import re
host="
**.**.**.**
"port=27017
conn = pymongo.mongoclient(host=host,port=port,tz_aware=false)
db_name = "
test
"log_name = "
col"
db_test =conn.get_database(db_name)
col_col =db_test.get_collection(log_name)
"db names:
", conn.database_names()
"col names:
", db_test.collection_names()
"one_line:
", col_col.find_one()
"one_line_keys:
", col_col.find_one().keys()
#col_col.insert()
#col_col.update(, })
import datetime
y,m,d,h,m,s = 2016,2,13,18,30,0
d =datetime.datetime(y,m,d,h,m,s)
delta = datetime.timedelta(hours=8
)d = d-delta
#logs = col_col.find( } )
#logs = col_col.find( )
logs = col_col.find( , }]} )
c =logs.count()
for l in
logs:
print l
"count:
", c
"end!
"
pymongo的使用總結
本地執行ez setup.py檔案,然後在環境變數path下加入可執行easy install的指令碼路徑.python安裝pymongo,easy install pymongo 我安裝的版本pymongo是3.0的,所以有些語法和網上找到的不太一樣,需要自己除錯 coding utf 8 imp...
python之PyMongo使用總結
個人分類 python pymongo是驅動程式,使python程式能夠使用mongodb資料庫,使用python編寫而成 環境 ubuntu 14.04 python2.7 mongodb 2.4 進行安裝 或者用pip安裝pip m install pymongo import pymongo ...
PyMongo基本使用
引用pymongo import pymongo 建立連線connection import pymongo conn pymongo.connection localhost 27017 或import conn connection localhost 27017 建立connection時,指...