【Python】pymongo連結mongo
pymongo模組連結mongo
1.連結單例項mongo
-
# - * - coding : utf - 8 - * -
-
import datetime
-
import pymongo
-
import time
-
-
client = pymongo . MongoClient ( "127.0.0.1" , 27017 )
-
db = client . get_database ( "collection" )
-
db . authenticate ( 'collection' , '78hRdJEnJcHRb4qA' )
-
#print ( db . collection_names ( ) )
-
redefine_collection = db . get_collection ( 'redefine-collection' )
alluser=[]
-
demos = redefine_collection . find ( { 'event_id' : '0109001' , 'event_info.url' : { '$exists' : 'true' } , "ctime" : { "$gte" : 1525881600000}} , no_cursor_timeout = True )
-
for item in demos :
-
if len ( item [ 'event_info' ] [ 'url' ] . replace ( r '/' , '' ) . split ( '.' ) [ - 1 ] ) < = 5 :
-
alluser . append ( item [ 'event_info' ] [ 'url' ] . replace ( r '/' , '' ) . split ( '.' ) [ - 1 ] )
-
demos . close ( )
-
print len ( alluser )
-
2.連線複製集
-
from pymongo import MongoClient
-
conn = MongoClient ( [ '192.168.3.11:27017' , '192.168.3.12:27017' , '192.168.3.13:27017' ],replicaset='shard1' )
-
from pymongo import ReadPreference
-
db = conn . get_database ( 'hnrtest' , read_preference = ReadPreference . SECONDARY_PREFERRED )
3.mongo常見操作
# #####read client # client = pymongo.MongoReplicaSetClient(['172.31.46.25:27017,172.31.43.36:27017,172.31.40.242:27017'],replicaset='shard1') # db = client.get_database("collection") # db.authenticate('collection', '78hRdJEnJcHRb4qA') # # print db.client.read_preference # print db.client.primary # print db.client.secondaries # print db.client.arbiters # print db.command('ismaster') #####read client client = pymongo.MongoClient(['172.31.32.223:20000']) db = client.get_database("admin") db.authenticate('admin', 'ggxP6tPI971K3W0r') # print db.client.read_preference # print db.client.primary # print db.client.secondaries # print db.client.arbiters # print db.client.is_primary # print db.command('ismaster') # print db.command('currentOp') # print db.command('replSetGetStatus') # print db.list_collection_names() statement=client.collection.get_collection('statement') ###獲取對應db下的對應集合 print statement.count() # client.collection.add_user('newTestUser', 'Test123', roles=[{'role':'readWrite','db':'collection'}]) ####新增使用者 # client.collection.remove_user('newTestUser') ####刪除使用者 db2 = client.get_database("collection") db2.add_user('newTestUser', 'Test123', roles=[{'role':'readWrite','db':'collection'}]) ###新增使用者 print db.current_op() # options = {'lock': True} # db.client.fsync(**options) # print db.client.is_locked # #### # print db.command('fsyncUnlock') # print db.client.is_locked
-
參考:http : / / blog . 51cto . com/hnr520/1874506
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2156378/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python 連線 mongo 資料庫連線超時PythonGo資料庫
- Mongo連線分析Go
- Python 全棧系列44 - pymongo的基本使用Python全棧Go
- Python安裝PyMongo的方法詳細介紹PythonGo
- [Python] 透過pymongo連線docker中並開啟了副本集的mongodb資料庫PythonDockerMongoDB資料庫
- 連結串列(python)Python
- MongoDB Python官方驅動 PyMongo 的簡單封裝MongoDBPython封裝
- Python3中使用PyMongo的方法詳解PythonGo
- Python目錄連結Python
- LeetCode-Python-86. 分隔連結串列(連結串列)LeetCodePython
- 【Mongo】mongo聚合操作Go
- Python 連結串列實踐Python
- 【Mongo】mongo配置檔案Go
- Python實現單連結串列Python
- python爬蟲之解析連結Python爬蟲
- MongoDB 及 PyMongo 的基本用法MongoDB
- MongoDB和pymongo自用手冊MongoDB
- 【Python】python連結串列應用原始碼示例Python原始碼
- Python爬蟲-獲得某一連結下的所有超連結Python爬蟲
- 連結串列找環(python實現)Python
- python資料結構——連結串列(無序列表)Python資料結構
- 【mongo】mongo 欄位型別互轉Go型別
- Python全棧MongoDB資料庫(聚合、二進位制、GridFS、pymongo模組)Python全棧MongoDB資料庫
- mongo GridFSBucketGo
- 資料結構-雙向連結串列(Python實現)資料結構Python
- 一個學習Python的好連結Python
- python環境連結Oracle資料庫PythonOracle資料庫
- LeetCode-Python-(206)反轉連結串列LeetCodePython
- 【Mongo】shell命令列模式執行mongo命令Go命令列模式
- 【Mongo】Mongo讀寫分離的實現Go
- 【連結 1】與靜態連結庫連結
- nuxt(vue) + koa + mongo 寫blog的一次小結UXVueGo
- 函式計算Python連線SQLServer小結函式PythonSQLServer
- linux 修改預設pip/python版本 軟連結LinuxPython
- Python實現環形連結串列詳解Python
- Python爬蟲批次下載電影連結Python爬蟲
- Python實現單向連結串列詳解Python
- mongo基本操作Go