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的更多相关文章
随机推荐
- Kademlia、DHT、KRPC、BitTorrent 协议、DHT Sniffer
catalogue . 引言 . Kademlia协议 . KRPC 协议 KRPC Protocol . DHT 公网嗅探器实现(DHT 爬虫) . BitTorrent协议 . uTP协议 . P ...
- JVM 内存初学 堆(heap)、栈(stack)和方法区(method)
这两天看了一下深入浅出JVM这本书,推荐给高级的java程序员去看,对你了解JAVA的底层和运行机制有比较大的帮助.废话不想讲了.入主题:先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(he ...
- Oracle DataBase 编码格式
sqlplus 查询 Oracle 数据库结果乱码或显示 ? 则需要设置字符集 一.客户端字符集 格式:NLS_LANG=language_territory.charset Language: 指定 ...
- 14、JDBC-DbUtils-API
DbUtils /** * DbUtils :提供如关闭连接.装载 JDBC 驱动等操作的工具类,里面方法都是静态的. * * public static void close(…) throws j ...
- 6.适配器模式(Adapter Pattern)
适配(转换)的概念无处不在......适配,即在不改变原有实现的基础上,将原先不兼容的接口转换为兼容的接口.例如:二转换为三箱插头,将高电压转换为低电压等. 动机(Motivate): 在 ...
- [NIO-3]Socket通道
Socket通道 上文讲述了通道.文件通道,这篇文章来讲述一下Socket通道,Socket通道与文件通道有着不一样的特征,分三点说: 1.NIO的Socket通道类可以运行于非阻塞模式并且是可选择的 ...
- HDU - 6313 Hack It(构造)
http://acm.hdu.edu.cn/showproblem.php?pid=6313 题意 让你构造一个矩阵使得里面不存在四个顶点都为1的矩形,并且矩阵里面1的个数要>=85000 分析 ...
- Eclipse创建SpringMVC,Spring, Hibernate项目
创建一个java project,创建连个folder,分别命名为java和webapp,删除src文件夹. 打开工程的propertise设置中的build to path,将输出设置为工程名\we ...
- GreenDao 直接执行SQL的方法
try { DaoSession session=MyApplication.getInstances().getDaoSession(); long fromId=-1; String strSql ...
- SpringBoot入门笔记(二)、使用fastjson
1.添加fastjson配置 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastj ...