# -*- 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. flask get和post请求使用

    直接看代码 #-*-coding:utf-8-*- from flask import Flask,url_for,redirect,render_template,request app = Fla ...

  2. maven的安装与基本使用

    一.什么是maven,有什么用? Maven是一个项目构建和管理的工具,其主要服务于基于java平台的项目构建.依赖管理和项目信息管理.它包含了一个项目对象模型 (Project Object Mod ...

  3. Linux记录-linux系统监控命令汇总

    命令 功能应用 用法举例     free 查看内存使用情况,包括物理内存和虚拟内存 free -h或free -m     vmstat 对系统的整体情况进行统计,包括内核进程.虚拟内存.磁盘.陷阱 ...

  4. Hbase记录-hbase部署

    #版本支持 #官网下载二进制包,解压到/usr/app下,配置/etc/profile: export HBASE_HOME=/usr/app/hbase export PATH=$HBASE_HOM ...

  5. ARM寄存器介绍

    ARM处理器共有37个寄存器.其中包括:31个通用寄存器,包括程序计数器(PC)在内.这些寄存器都是32位寄存器.以及6个32位状态寄存器.但目前只使用了其中12位.ARM处理器共有7种不同的处理器模 ...

  6. 细说ORM之Entity FrameWork系列(被替换)

    一. 谈情怀 从第一次接触开发到现在(2018年),接近五年时间了,最初阶段连接数据库,使用的是[SQL语句+ADO.NET],那时候,什么存储过程.什么事务 统统不理解,生硬的将SQL语句传入SQL ...

  7. 032、学容器必须懂bridge网络(2019-02-19 周二)

    参考https://www.cnblogs.com/CloudMan6/p/7066851.html   docker安装时会创建一个名为 docker0 的linuxbridge.如果不指定 --n ...

  8. [Android] Sqlite 数据库操作 工具封装类

    sqlite 数据库封装类 DatabaseUtil.java(封装的类) package com.jack.androidbase.tools; import android.content.Con ...

  9. spring注解第05课 FactoryBean

    1.工厂bean调用 @Configuration public class MainConfig2 {/** * 使用Spring提供的 FactoryBean(工厂Bean); * 1).默认获取 ...

  10. Hyperic-Sigar简介——检测与监控

    http://blog.csdn.net/liyong199012/article/details/20302761 Hyperic-Sigar是一个收集系统各项底层信息的工具集.他有如下特点: 1. ...