Python Pymongo中Connection()与MongoClient()差异

在git找了几个blog的源码,在学习的过程中,发现有人使用Connection(),有人却在使用MongoClient(),那么到底两者有什么差别呢?

且看分析如下:

db = Connection(‘192.168.1.101‘, 27017).performance_test

#client = MongoClient(‘192.168.1.101‘, 27017)

#db = client.performance_test 

 db.drop_collection("updates") 

collection = db.updates 

collection.insert({"x": 1}) 

collection.find_one() 

start = time.time() 

for i in range(100000): 

        collection.update({}, {"$push" : {"x" : 1}}) 

...

运行结果:

>python test_mongo_conn.py

8.43799996376

>python test_mongo_client.py

62.5780000687

用Connection() 8秒,MongoClient()则花了62秒,这性能相差也太多了。

很是疑惑,于是查了pymongo的文档,原来两者有个选项的默认行为不同:

class pymongo.connection.Connection([host=‘localhost‘[, port=27017[, max_pool_size=10[, network_timeout=None[, document_class=dict[, tz_aware=False[, **kwargs]]]]]]])

Write Concern options:

safe: Connection disables acknowledgement of write operations. Use safe=True to enable write acknowledgement.

w: (integer or string) If this is a replica set, write operations will block until they have been replicated to the specified number or tagged set of servers. w=<int> always includes the replica set primary (e.g. w=3 means write to the primary and wait until replicated to two secondaries). Implies safe=True.

wtimeout: (integer) Used in conjunction with w. Specify a value in milliseconds to control how long to wait for write propagation to complete. If replication does not complete in the given timeframe, a timeout exception is raised. Implies safe=True.

j: If True block until write operations have been committed to the journal. Ignored if the server is running without journaling. Implies safe=True.

fsync: If True force the database to fsync all files before returning. When used with j the server awaits the next group commit before returning. Implies safe=True.

safe选项决定操作是“瞬时完成”与“安全操作”,connection()默认是safe=False,即瞬时完成,不等服务器回应,而MongoClient()默认是safe=True,即安全操作,等服务器确认后才继续下一步操作。

所以一个8秒,一个62秒,这个差距实际上是“瞬时完成”与“安全操作”两者的性能差别。

当将Connection() 和MongoClient()建立连接时指定相同的safe参数,两者的性能表现是一样的。

client = MongoClient(‘192.168.1.101‘, 27017,safe=False)

#db = Connection(‘192.168.1.101‘, 27017,safe=False).performance_test

结论:Python用Connection()连接时,修改操作速度非常快,而用MongoClient()建立的连接,操作速度慢很多。

顺便分享账号密码登录的代码:



from pymongo import MongoClient
client = MongoClient(‘www.yeayee.com‘,‘27017‘)
client.database.authenticate("user","password")
db = client.database
collection = db.collection
时间: 2024-08-18 23:36:07

Python Pymongo中Connection()与MongoClient()差异的相关文章

pymongo中的连接操作:Connection()与MongoClient()

class MongoClient(pymongo.common.BaseObject) Connection to MongoDB. Method resolution order: MongoClient pymongo.common.BaseObject __builtin__.object class Connection(pymongo.mongo_client.MongoClient) Connection to MongoDB. Method resolution order: C

几招学会 Python 3 中 PyMongo 的用法

本文和大家分享的是Python3下MongoDB的存储操作相关内容,在看本文之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库.下面进入正题,一起来看看吧,希望对大家学习Python3有所帮助. 连接MongoDB 连接MongoDB我们需要使用PyMongo库里面的MongoClient,一般来说传入MongoDB的IP及端口即可,第一个参数为地址host,第二个参数为端口port,端口如果不传默认是27017. import pymongo cl

在Python应用中使用MongoDB

Python是开发社区中用于许多不同类型应用的强大编程语言.很多人都知道它是可以处理几乎任何任务的灵活语言.因此,在Python应用中需要一个什么样的与语言本身一样灵活的数据库呢?那就是NoSQL,比如MongoDB. 英文原文:https://realpython.com/blog/python/introduction-to-mongodb-and-python 1.SQL vs NoSQL 如果你不是很熟悉NoSQL这个概念,MongoDB就是一个NoSQL数据库.近几年来它越来越受到整个

pymongo 中的模糊查询以及以某个值开始的模糊查询【pymongo $regex /^】

说明:主要是mongodb数据库的客户端中的shell命令查询和Python中的查询语法有些诧异: 模糊查询诧异: shell中: db.getCollection('郑州').find({community_name_pinyin:/^search_value/})[查询以search_value开始的数据] db.getCollection('郑州').find({community_name_pinyin:/search_value/})[查询字段中包含search_value的数据] p

python类中super()和__init__()的区别

本文和大家分享的主要是python开发中super()和__init__()的区别,希望通过本文的分享,对初学者学习这部分内容有所帮助. 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(self): print 'Base create' class childA(Base): def __init__(self): print 'creat A ', Base.__init__(self) class chi

python 正则表达式中反斜杠(\)的麻烦和陷阱

这里是一点小心得:由于下面两个原因,在正则表达式中使用反斜杠就会产生了一个双重转换的问题.(1).python自身处理字符串时,反斜杠是用于转义字符 (2).正则表达式也使用反斜杠来转义字符     要匹配字符串中1个反斜杠应该怎么写正则表达式?"\\",这样行吗?试试就知道了,re模块抛异常了,因为在正则表达式中,"\\"就是一个反斜杠,对于正则表达式解析器来说,是一个转义字符,但是后面啥也没有,自然就报错了,"\\\"三个肯定是不行的,试试四

如何从python代码中直接访问Android的Service

在Kivy中,通过pyjnius扩展可以间接调用Java代码,而pyjnius利用的是Java的反射机制.但是在Python对象和Java对象中转来转去总让人感觉到十分别扭.好在android提供了binder这个进程间通信的功能,Java中的Service也是基于Binder的C++代码封装来实现进程间通信的,这也为从Python代码中绕开pyjnius直接访问Java代码提供了可能,既然Java的Service是基于C++的封装来实现的,也同样可以在Python中封装同样的C++代码,这篇文

将SCONS工具集成到Python代码中

SCONS是Python的自动智能结构化编译工具,将来或许能代替Make. 在Windows或者Linux下,SConstruct文件相当于MakeFile,使用SCONS编译,需输入scons.bat(scons),后面带上编译选项options(如SConstruct所在的路径).观察scons.bat这个脚本,可以在其他Python代码中,将SCONS工具当作一个函数来调用.这个函数就是Scons.Script.main(),这个main函数将解析命令行中的编译选项,通过sys.argv[

《python源码剖析》笔记 python虚拟机中的一般表达式

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.字节码指令 LOAD_CONST:从consts表中读取序号为i的元素并压入到运行时栈中 STORE_NAME:改变local名字空间.从符号表names取序号为i的元素作为变量名, 取运行时栈的栈顶元素作为变量值,完成从变量名到变量值的映射关系的创建. BUILD_MAP:创建一个空的PyDictObject对象,并压入运行时栈 DUP_TOP:将栈顶元素的引用计数增加1,并将它再次