#
-
*
-
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
)
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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。