async_mongo_helper
# -*- coding: utf-8 -*-
# @Time : 2019/1/7 2:11 PM
# @Author : cxa
# @File : motortesdt.py
# @Software: PyCharm
import motor.motor_asyncio
import asyncio
import pprint
from bson import SON
db_configs = {
'type': 'mongo',
'host': '123.22.3.11',
'port': '27017',
'user': 'admin',
'passwd': '12344',
'db_name': 'spider_data'
}
class Motor():
def __init__(self):
self.__dict__.update(**db_configs)
self.motor_uri = f"mongodb://{self.user}:{self.passwd}@{self.host}:{self.port}/{self.db_name}?authSource={self.user}"
self.client = motor.motor_asyncio.AsyncIOMotorClient(self.motor_uri)
self.db = self.client.spider_data
async def do_insert(self):
document = {'key': 'value'}
result = await self.db.表名.insert_one(document)
print(f'result{result.inserted_id}')
async def do_find_one(self):
document = await self.db.表名.find_one({})
pprint.pprint(document)
async def do_find(self):
# cursor = db.表名.find({})
# for document in await cursor.to_list(length=2):
# pprint.pprint(document)
c = self.db.表名.find({})
c.sort('value', -1).limit(2).skip(1)
async for item in c:
pprint.pprint(item)
async def do_replace(self):
try:
coll = self.db.表名
old_document = await coll.find_one({'value': "50"})
print(f'found document:{pprint.pformat(old_document)}')
_id = old_document['_id']
old_document["value"] = "12"
result = await coll.replace_one({'_id': _id}, old_document)
print(result)
new_document = await coll.find_one({'_id': _id})
print(new_document)
except TypeError as e:
print("找不到value为50的数据")
async def do_update(self):
try:
condition = {"value": 50}
coll = self.db.表名
item = await coll.find_one(condition)
item["value"] = 10
result = await coll.update_one(condition, {'$set': item}, upsert=True)
# 更新多条update_many
print(f'updated {result.modified_count} document')
new_document = await coll.find_one(condition)
print(f'document is now {pprint.pformat(new_document)}')
except TypeError as e:
print("找不到value为50的数据")
async def do_delete_many(self):
coll = self.db.表名
n = await coll.count()
print('%s documents before calling delete_many()' % n)
result = await self.db.test_collection.delete_many()
print('%s documents after' % (await coll.count()))
async def use_count_command(self):
response = await self.db.command(SON([("count", "表名")]))
print(f'response:{pprint.pformat(response)}')
if __name__ == '__main__':
m = Motor()
loop = asyncio.get_event_loop()
loop.run_until_complete(m.use_count_command())
async_mongo_helper的更多相关文章
随机推荐
- 网络编程基础【day10】:进程与线程介绍(一 )
本节内容 1.概述 2.什么是进程? 3.什么是线程? 4.什么是携程? 5.存在的疑问 6.总结 一.概述 我们知道,所有的指令的操作都是有CPU来负责的,cpu是来负责运算的.OS(操作系统) 调 ...
- Java Web之HTML5
终于学到Java Web这一章节了,首先来了解一下HTML5的一些新知识点吧,我直接贴出HTML5代码看一下: <!DOCTYPE html> <html lang="en ...
- netty的对象传输
pom <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <grou ...
- Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization(第一周)深度学习的实践层面 (Practical aspects of Deep Learning)
1. Setting up your Machine Learning Application 1.1 训练,验证,测试集(Train / Dev / Test sets) 1.2 Bias/Vari ...
- 052、overlay如何实现跨主机通信?(2019-03-19 周二)
参考https://www.cnblogs.com/CloudMan6/p/7305989.html 今天开始学习 overlay 网络跨主机通信的原理 root@host01:~# ufw ...
- MyEclipse2017 隐藏回车换行符
Preferences->Text Editor->Show Whitespace characters(configure visibility)->Transparency Le ...
- Quartz.NET 配置文件详解
Ø 前言 在之前的 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务 文章中,使用到了 Quartz 的两个配置文件 quartz.config 和 quartz_jo ...
- GCC编译器原理(三)------编译原理三:编译过程(3)---编译之汇编以及静态链接【2】
4.1.2 符号解析与重定位 (1)重定位 在完成空间和地址的分配步骤之后,链接器就进入了符号解析和重定位的步骤,这是静态链接的核心部分. 先看看 a.o 的反汇编文件: objdump -d a.o ...
- mysql数据库允许远程连接
1.验证初始是否允许远程连接 由于本次虚拟机IP为192.168.2.120,因此我们执行 mysql -h 192.168.20.120 -P 3306 -u root -proot(备注:-pro ...
- MySQL图形化管理工具
PHPMyAdmin(关于web界面的) Navicat MySQL Workbench