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:
Connection
pymongo.mongo_client.MongoClient
pymongo.common.BaseObject
__builtin__.object

我们先看一下源码,从这两个类的继承来看,connection是继承了MongoClient的,建议使用MongoClient而不是使用Connection。(也就是说,MongoClient可以使用方法Connection都可以使用)

from pymongo import MongoClient
client = MongoClient('192.168.40.87', 27037)
db_name = 'TCL_Useraction'
db = client[db_name]
collection_useraction = db['useraction']

这里是通过字典的方式访问数据库和集合,同时你也可以通过.(点号)的方式访问

做实验时,发现Python用Connection()连接时,修改操作速度非常快,而用MongoClient()建立的连接,操作速度慢很多。

from pymongo import MongoClient,Connection
import time
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)

---------------------
作者:djd已经存在
来源:CSDN
原文:https://blog.csdn.net/djd1234567/article/details/47859015
版权声明:本文为博主原创文章,转载请附上博文链接!

pymongo中的连接操作:Connection()与MongoClient()的更多相关文章

  1. spark中各种连接操作以及有用方法

    val a = sc.parallelize(Array(("123",4.0),("456",9.0),("789",9.0)) val ...

  2. 一起学Hadoop——实现两张表之间的连接操作

    ---恢复内容开始--- 之前我们都是学习使用MapReduce处理一张表的数据(一个文件可视为一张表,hive和关系型数据库Mysql.Oracle等都是将数据存储在文件中).但是我们经常会遇到处理 ...

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

    在git找了几个blog的源码,在学习的过程中,发现有人使用Connection(),有人却在使用MongoClient(),那么到底两者有什么差别呢? 且看分析如下: db = Connection ...

  4. JAVA中通过Jedis操作Redis连接与插入简单库

    一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...

  5. php大力力 [024节]PHP中的字符串连接操作(2015-08-27)

    2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作  阅读:次   时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...

  6. python 连接操作 各类数据库

    转载自MySQL Loners 一,python 操作 MySQL:详情见:这里 #!/bin/env python # -*- encoding: utf-8 -*- #-------------- ...

  7. mongoDB中的连接池(转载)

    一.mongoDB中的连接池 刚上手MongoDB,在做应用时,受以前使用关系型数据库的影响,会考虑数据库连接池的问题! 关系型数据库中,我们做连接池无非就是事先建立好N个连接(connection) ...

  8. 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    Min Pool Size的理解是错误的 假设我们在一个ASP.NET应用程序的连接字符串中将Min Pool Size设置为30: <add name="cnblogs" ...

  9. 在Tomcat中配置连接池和数据源

    1.DataSource接口介绍 (1)DataSource 概述 JDBC1.0原来是用DriverManager类来产生一个对数据源的连接.JDBC2.0用一种替代的方法,使用DataSource ...

随机推荐

  1. node.js学习6---第三方依赖(模块或者说是包)的导入 npm 以及 cnpm命令的使用

    npm命令用于导入node.js的第三方包,相当于java中使用maven来导入第三方依赖: 1.npm init -y 命令:在命令窗口中执行后,会出现如下的json文件: 右边记录了安装的第三方包 ...

  2. Java randomString

    public static String randomString(int strLength) { Random rnd = ThreadLocalRandom.current(); StringB ...

  3. 20175224 2018-2019-2 《Java程序设计》第四周学习总结

    教材学习内容总结 子类的定义 class 子类名 extends 父类名 { ... } 子类继承性 子类继承父类的成员变量.方法.成员变量可以被子类中自己定义的任何实例方法操作:继承的方法可以被子类 ...

  4. 分布式session个人理解浅谈

    在分布式中,用户的session如何处理呢? 服务器中的原生session是无法满足需求的,因为用户的请求有可能随机落入到不同的服务器中,这样的结果将会导致用户的session丢失,传统做法中有解决方 ...

  5. Gurobi在Python环境里安装与使用(Windows环境)

  6. oracle中创建数据库

    一.在Oracle中创建数据库之前先改一下虚拟机的IP地址,以便访问 2. 3. 3.1 3.2 3.3 3.4 创建完成:输入sqlplus sys/123456 as sysdba测试

  7. ubuntu 下redis的安装简介

    Linux公社:https://www.linuxidc.com/topicnews.aspx?page=2&tid=2 简单介绍下ubuntu下redis的安装方式: 第一种: 1:进入re ...

  8. js 防抖 debounce 与 节流 throttle

    debounce(防抖) 与 throttle(节流) 主要是用于用户交互处理过程中的性能优化.都是为了避免在短时间内重复触发(比如scrollTop等导致的回流.http请求等)导致的资源浪费问题. ...

  9. wpf binging (六)多绑定

    场景 比如我用四个textbox 需要每个控件都输入正确的数据以后 下方的 button才变成可用状态 需要把四个textbox的值转换成 true或者false 效果

  10. centos7 安装mysql出现Could NOT find Curses (missing CURSES_LIBRARY CURSES_INCLUDE_PATH)

    今天安装mysql 5.7 编译时出现一下问题: [root@localhost software]# cd mysql-5.7.21 [root@localhost mysql-5.7.21]# c ...