pymongo

Get total users list along with the active users in mongodb matched on OR condition

不想你离开。 提交于 2020-08-08 05:13:25
问题 Table : user: A, active_1 : "true", active_2 : "false", is_user : "true" user: B, active_1 : "false", active_2 : "true", is_user : "true" user: C, active_1 : "false", active_2 : "false", is_user : "true" Expected Output: { "_id" : null, "total" : 3, "count" : 2 } I need to check either the active_1 or active_2 is true and get the output as like total which indicates the total no.of users which is A,B and C. The count indicates the users who have either active_1 or active_2 is true. It should

pymongo的基本使用

痞子三分冷 提交于 2020-08-06 08:58:04
一、链接数据库   # 链接数据库se7en521是账号,123456是密码,211.159.185.88是地址,27017是端口号   client = MongoClient('mongodb://se7en521:123456@211.159.185.88:27017')   # 指定需要链接的数据库   mongo_DB = client['video']   # 指定需要操作的数据库中的表   video_old = mongo_DB.video_old 二、增   一、增(插入单条,系统已经不推荐使用)   result1 = video_old.insert({'vid':'10086','category':"111.1.1_1.1",'type':'3','title':'test'})   print('result1=%s'%result1)   print(type(result1))   # 类型是ObjectID类型,及返回值是_id   # result1 = 5ee2e5585979c83dd911d1ca   # <class 'bson.objectid.ObjectId'>   二、增(插入多条,系统已经不推荐使用)   result2 = video_old.insert([{'vid':'10087','category':"111.1.1

手把手教你给行情收集器升级回测自定义数据源功能

江枫思渺然 提交于 2020-08-06 08:27:22
上一篇文章 手把手教你实现一个行情收集器 我们一起实现了一个收集行情的机器人程序,收集了行情数据接下来怎么使用呢?当然是用于回测系统了,这里依托于发明者量化交易平台回测系统的自定义数据源功能,我们就可以直接把收集到的数据作为回测系统的数据源,这样我们就可以让回测系统应用于任何我们想回测历史数据的市场了。 因此,我们可以给「行情收集器」来一个升级!让行情收集器同时可以作为自定义数据源给回测系统提供数据。 有了需求,动手! 准备 和上次文章中的准备工作有所不同,上一次是在我的本机MAC电脑上运行的托管者程序,安装mongodb数据库启动数据库服务。这次我们把运行环境换到VPS上,使用阿里云linux服务器,来运行我们这一套程序。 mongodb数据库 和上篇文章一样,需要在行情收集器程序运行的设备上安装mongodb数据库,并且开启服务。和在MAC电脑上安装mongodb基本一样,网上有不少教程,可以搜索看下,很简单。 安装python3 程序使用python3语言,注意用到了一些库,没有的话需要安装。 pymongo http urllib 托管者 运行一个发明者量化交易平台的托管者即可。 改造「行情收集器」 行情收集器即 RecordsCollecter (教学) 这个策略。 我们来对它做一些改造: 在程序进入收集数据的while循环之前,使用多线程库,并发执行启动一个服务

第78天: Python 操作 MongoDB 数据库介绍

房东的猫 提交于 2020-07-26 03:50:34
by 極光 MongoDB 是一款面向文档型的 NoSQL 数据库,是一个基于分布式文件存储的开源的非关系型数据库系统,其内容是以 K/V 形式存储,结构不固定,它的字段值可以包含其他文档、数组和文档数组等。其采用的 BSON (二进制 JSON )的数据结构,可以提高存储和扫描效率,但空间开销会有些大。今天就为大家简单介绍下在 Python 中使用 MongoDB 。 安装 PyMongo 库 在 Python 中操作 MongoDB ,需要使用 PyMongo 库,执行如下命令安装: pip3 install pymongo 连接 MongoDB 数据库 连接时需要使用 PyMongo 库里面的 MongoClient 模块,有两种方式可以创建连接,默认只需要传入IP和端口号即可。如果数据库存在账号密码,则需要指定连接的数据库,并进行鉴权才能连接成功。 #导入 MongoClient 模块 from pymongo import MongoClient, ASCENDING, DESCENDING # 两种方式 #1. 传入数据库IP和端口号 mc = MongoClient('127.0.0.1', 27017) #2. 直接传入连接字串 mc = MongoClient('mongodb://127.0.0.1:27017') # 有密码的连接 #

pip install pymongo SSLError

假如想象 提交于 2020-07-24 21:15:17
pip3 install pymongo 出现SSL error Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:877)'),)': /packages/95/8c/83563c60489954e5b80f9e2596b93a68e1ac4e4a730deb1aae632066d704/openpyxl-3.0.3.tar.gz 找了几个国内的源 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(douban) http://pypi.douban.com/simple/ 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/ pip install openpyxl -

Merge Mongodb collection and a Python Dict

北战南征 提交于 2020-07-10 07:03:25
问题 I have a collection that look something like that : { {"name": "aaa", "value": 100}, {"name": "bbb", "value": 50}, {"name": "ccc", "value": 200}, } and imagine I have a dict like this one : { {"name": "aaa", "value": 40}, {"name": "ccc", "value": -100}, {"name": "ddd", "value": 200}, } I would like to merge the dict into the collections in a way it add the documents in which the name does not appear in the collection, and merge the 2 value between documents that had a name equal. The

Use MongoEngine and PyMongo together

时间秒杀一切 提交于 2020-07-04 06:23:22
问题 I want to use MongoEngine for my next project. Now I'm wondering whether I could also use PyMongo directly in the same project. Just for the case that I need something very special that is not supported directly via mongoengine. Are there any doubts that this would work, or that I should not do that!? 回答1: Author of MongoEngine here - MongoEngine is built upon pymongo so of course you can drop into pymongo - or use raw pymongo in your code! There are some document helpers that allow you to

Use MongoEngine and PyMongo together

只谈情不闲聊 提交于 2020-07-04 06:22:17
问题 I want to use MongoEngine for my next project. Now I'm wondering whether I could also use PyMongo directly in the same project. Just for the case that I need something very special that is not supported directly via mongoengine. Are there any doubts that this would work, or that I should not do that!? 回答1: Author of MongoEngine here - MongoEngine is built upon pymongo so of course you can drop into pymongo - or use raw pymongo in your code! There are some document helpers that allow you to

How to run raw mongodb commands from pymongo

最后都变了- 提交于 2020-07-02 07:29:28
问题 In a mongo command line I can run db.my_collection.stats() I need to get my collections stats from Python so I tried from pymongo import MongoClient client = MongoClient() db = client.test_database collection = db.test_collection collection.stats() But I get TypeError: 'Collection' object is not callable. If you meant to call the 'stats' method on a 'Collection' object it is failing because no such method exists. This is because pymongo does not support this method. How do I send raw mongoDB

Insert many documents into empty collection, update if document with same key already exist for mongodb

狂风中的少年 提交于 2020-06-29 04:36:06
问题 I have this list of dictionaries json_data json_data = [ { "CODE": "018906", "X": "29813.66349", "Y": "28697.520760000003" }, { "CODE": "018907", "X": "30041.8389", "Y": "28602.98724" }, { "CODE": "018910", "X": "31966.120789999997", "Y": "29115.75337" }, ] I have this mongodb collection code_col . I want to insert json_data into collection code_col when the collecion is empty. There may be a new json_data next time and if the key CODE is the same, the document should be updated instead of