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. content-type的几种取值

    四种常见的 POST 提交数据方式 我们知道,HTTP 协议是以 ASCII 码传输,建立在 TCP/IP 协议之上的应用层规范.规范把 HTTP 请求分为三个部分:状态行.请求头.消息主体.类似于下 ...

  2. MySQL创建新用户以及ERROR 1396 (HY000)问题解决

     登 录mysql mysql -u root -p 创建允许本地 IP访问localhost的Mysql数据库时出错 create user 'lijing'@'localhost' identif ...

  3. pytest自动化5:pytest-html插件--生成html报告

    前言:pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.  pip 安装: pip install pytes ...

  4. 淘宝的推荐系统 计算之道2A

    小明刚刚入职淘宝,老大给他交代了一个简单的任务,实现一个简易的商品推荐系统. 这个商品推荐系统的需求如下: 一共有 n 件商品可以被推荐,他们的编号分别为 1 到 n.每件商品都有一个价格,编号为 i ...

  5. C++标准库之迭代器

    迭代器大致可分为: 输入迭代器,InputIterator 输出迭代器,OutputIterator 前行迭代器,ForwardIterator 双向迭代器,BidirectinalIterator ...

  6. 【原创】Proxmark3系列教程1——PM3用法

    1 PM3介绍 proxmark3是一款开源的RFID安全研究平台黑色按钮从图中我们可以看到左上方有一颗黑色按钮,这个按钮就是Proxmark3的功能键,主要用于启动嗅探模式以及停止进程功能,其中内置 ...

  7. 29.求3x3的整数矩阵对角线元素之和

    #include <stdio.h> #include <stdlib.h> int main() { ,a[][]; ;i<;i++) { ;j<;j++) sc ...

  8. loadrunner11浏览器兼容性的问题

    最近项目中遇到了新开发的系统,全是HTML5和一些最新的前端框架技术,由于没有做浏览器兼容处理,所以该系统无法在IE浏览器进行操作,对firefox和google浏览器支持较好.但是又一个问题出现了, ...

  9. int 和 Integer

    现状1+1=?,不加思索2.有一个数字要存储在程序里,不加思索int.那为什么java要弄一个Integer类型出来?有什么用?怎么用?差别在哪儿?度娘说java提供了两种数据类型,一种是值类型,一种 ...

  10. IDEA 自动生成Hibernate实体类和Mapping文件

    一.新建工程Demo(如果选的时候勾选了hibernate,IDEA会自动下载Hibernate包,不需要手动导入) 二.导入相关包 Mysql && Hibernate 三.添加Hi ...