# -*- 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的更多相关文章

随机推荐

  1. 设计模式---接口隔离模式之中介者模式(Mediator)

    一:概念 在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互.Mediator对象起到控制器的作用 二:动机 在软件构建的过程中, ...

  2. Hadoop记录-Linux Service

    [Unit] Description=Datanode After=syslog.target network.target auditd.service sshd.service datanode_ ...

  3. shop++改造之Filter类

    基于shop++源码进行商城改造.本来想大展手脚,结果一入手.发觉瞬间淹没了我的才华,sql语句也得贼溜没啥用. 不得不说这个商城源码价值很高,封装的很精屁. 下面是我第一天入手的坑. 数据库建好了表 ...

  4. idea创建父子工程

    第一步:创建一个新的父工程father:file—–>new—->project ,注意要选maven,Create from archetype不要勾选.next填写GroupId .A ...

  5. Mastering Markdown

    What is markdown? Markdown is a lightweight and easy-to-use syntax for styling all forms writing on ...

  6. windows安装gitblit服务端

    由于windows下没有gitlab之类的工具,只有很久没有更新的gitblit 下载Gitblit, 下载地址:http://www.gitblit.com/ 很长时间没有更新了,在没有linux环 ...

  7. Leetcode 136 Single Number 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number/ 题目描述Given an array of integers, every element appea ...

  8. 细说tomcat之集群session共享方案

    1. Tomcat Cluster官网:http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.htmlTomcat原生支持的集群方案,通过组播消息 ...

  9. Python安全 - 从SSRF到命令执行惨案

    前两天遇到的一个问题,起源是在某个数据包里看到url=这个关键字,当时第一想到会不会有SSRF漏洞. 以前乌云上有很多从SSRF打到内网并执行命令的案例,比如有通过SSRF+S2-016漏洞漫游内网的 ...

  10. 002_Add Two Numbers

    # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # sel ...