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的更多相关文章
随机推荐
- python django基础四 ORM简介
ORM,全称是object relation mapping.翻译过来,就是对象关系映射. 主要来学习MySQL操作,MySQL是一个软件.它的优点:1.免费 2.开源 pymysql,就是Mysql ...
- 阿里云MongoDB存储数据
近期上了个活动,考虑后期的运维及人力成本,还是选择了阿里云的MongoDB,不过阿里云这玩意本地测试官方没有给本地测试链接地址,只能做映射上去了测了... 选了个2核4G的,更多详细信息,可以去阿里上 ...
- Linux记录-shell实现脚本监控服务器及web应用
1.apache web 服务器 1 2 3 4 5 6 7 8 9 10 !/bin/bash # 表示请求链接3秒钟,不要返回的测试数据 nc -w 3 localhost 80 &> ...
- [JUC-1]并发包实现及线程状态
一.Java 并发包实现 二.Java 线程状态转换图
- [JVM-1]Java运行时数据区域
Java虚拟机(JVM)内部定义了程序在运行时需要使用到的内存区域 这些区域都有自己的用途,以及创建和销毁的时间.有些区域随着虚拟机进程的启动而存在,有的区域则依赖用户线程的启动和结束而销毁和建立. ...
- I/O exception (java.net.SocketException) caught when processing request: Connect
Exception [一个故障引发的话题] 最近,项目中的短信模块收到一个故障日志,要求我协助调查一下: 2010-05-07 09:22:07,221 [?:?] INFO httpclient. ...
- [leetcode-117]填充每个节点的下一个右侧节点指针 II
(1 AC) 填充每个节点的下一个右侧节点指针 I是完美二叉树.这个是任意二叉树 给定一个二叉树 struct Node { int val; Node *left; Node *right; Nod ...
- HashMap 集合的遍历
HashMap 集合的遍历: 两种方式遍历HashMap: //集合hashMap的遍历: //方式一: @Test public void testMethod1(){ HashMap<Str ...
- C++模板的使用以及常见问题
最近的数据结构实验频繁地遇到了模板,之前对这一块接触不多,遇到了很多问题,放到这里总结一下. 模板的声明有两种:template <typename Type>或者template< ...
- Kibana 搜索语法
Kibana 搜索语法 Kibana 支持三种搜索语法, 分别是 Lucene query 语法, 基于 json 的 ES query语法, 以及 Kuery 语法. 前两种语法可以直接使用, Ku ...