ITPub博客

【Python】pymongo链接mongo

原创 作者:小亮520cl 时间:2018-06-20 14:29:50 0 删除 编辑

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'])
  3. from pymongo import ReadPreference
  4. db = conn.get_database('hnrtest', read_preference=ReadPreference.SECONDARY_PREFERRED)

  5. 参考:http://blog.51cto.com/hnr520/1874506


请登录后发表评论 登录
全部评论

注册时间:2013-09-12

  • 博文量
    514
  • 访问量
    936074