热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【Python】pymongo链接mongo

pymongo模块链接mongo1.链接单实例mongo#-*-coding:utf-8-*-importdatetimeimport

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 [ &#39;event_info&#39; ] [ &#39;url&#39; ] . replace ( r &#39;/&#39; , &#39;&#39; ) . split ( &#39;.&#39; ) [ - 1 ] ) < = 5 :

  4.         alluser . append ( item [ &#39;event_info&#39; ] [ &#39;url&#39; ] . replace ( r &#39;/&#39; , &#39;&#39; ) . split ( &#39;.&#39; ) [ - 1 ] )

  5. demos . close ( )

  6. print len ( alluser )




2.连接复制集

  1. from pymongo import MongoClient

  2. conn = MongoClient ( [ &#39;192.168.3.11:27017&#39; , &#39;192.168.3.12:27017&#39; , &#39;192.168.3.13:27017&#39; ],replicaset=&#39;shard1&#39; )

  3. from pymongo import ReadPreference

  4. db = conn . get_database ( &#39;hnrtest&#39; , read_preference = ReadPreference . SECONDARY_PREFERRED )


3.mongo常见操作

# #####read client
# client = pymongo.MongoReplicaSetClient([&#39;172.31.46.25:27017,172.31.43.36:27017,172.31.40.242:27017&#39;],replicaset=&#39;shard1&#39;)
# db = client.get_database("collection")
# db.authenticate(&#39;collection&#39;, &#39;78hRdJEnJcHRb4qA&#39;)
#
# print db.client.read_preference
# print db.client.primary
# print db.client.secondaries
# print db.client.arbiters
# print db.command(&#39;ismaster&#39;)
#####read client
client = pymongo.MongoClient([&#39;172.31.32.223:20000&#39;])
db = client.get_database("admin")
db.authenticate(&#39;admin&#39;, &#39;ggxP6tPI971K3W0r&#39;)
# 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(&#39;ismaster&#39;)
# print db.command(&#39;currentOp&#39;)
# print db.command(&#39;replSetGetStatus&#39;)
# print db.list_collection_names()
statement=client.collection.get_collection(&#39;statement&#39;)             ###获取对应db下的对应集合
print statement.count()
# client.collection.add_user(&#39;newTestUser&#39;, &#39;Test123&#39;, roles=[{&#39;role&#39;:&#39;readWrite&#39;,&#39;db&#39;:&#39;collection&#39;}])  ####添加用户
# client.collection.remove_user(&#39;newTestUser&#39;)  ####删除用户
db2 = client.get_database("collection")
db2.add_user(&#39;newTestUser&#39;, &#39;Test123&#39;, roles=[{&#39;role&#39;:&#39;readWrite&#39;,&#39;db&#39;:&#39;collection&#39;}])  ###添加用户
print db.current_op()
# options = {&#39;lock&#39;: True}
# db.client.fsync(**options)
# print db.client.is_locked
# ####
# print db.command(&#39;fsyncUnlock&#39;)
# print db.client.is_locked
  1. 参考:http : / / blog . 51cto . com/hnr520/1874506




推荐阅读
author-avatar
从妖妖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有