【Python】pymongo链接mongo-成都快上网建站

【Python】pymongo链接mongo

pymongo模块链接mongo

1.链接单实例mongo

  1. # - * - coding : utf - 8 - * -

    站在用户的角度思考问题,与客户深入沟通,找到隆子网站设计与隆子网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、成都做网站、企业官网、英文网站、手机端网站、网站推广、空间域名、虚拟主机、企业邮箱。业务覆盖隆子地区。

  2. import datetime

  3. import pymongo

  4. import time

  5. client = pymongo . MongoClient ( "127.0.0.1" , 27017 )

  6. db = client . get_database ( "collection" )

  7. db . authenticate ( 'collection' , '78hRdJEnJcHRb4qA' )

  8. #print ( db . collection_names ( ) )

  9. redefine_collection = db . get_collection ( 'redefine-collection' )

alluser=[]

  1. demos = redefine_collection . find ( { 'event_id' : '0109001' , 'event_info.url' : { '$exists' : 'true' } , "ctime" : { "$gte" : 1525881600000}} , no_cursor_timeout = True )

  2. for item in demos :

  3.      if len ( item [ 'event_info' ] [ 'url' ] . replace ( r '/' , '' ) . split ( '.' ) [ - 1 ] ) < = 5 :

  4.         alluser . append ( item [ 'event_info' ] [ 'url' ] . replace ( r '/' , '' ) . split ( '.' ) [ - 1 ] )

  5. demos . close ( )

  6. print len ( alluser )


2.连接复制集

  1. from pymongo import MongoClient

  2. conn = MongoClient ( [ '192.168.3.11:27017' , '192.168.3.12:27017' , '192.168.3.13:27017' ],replicaset='shard1' )

  3. from pymongo import ReadPreference

  4. 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
  1. 参考:http : / / blog . 51cto . com/hnr520/1874506



本文题目:【Python】pymongo链接mongo
文章起源:http://kswjz.com/article/pgechs.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流