pgcr-mongo 1.0.1
python操作MongoDB数据库的简单工具，支持异步，可以结合FastAPI使用
License: MIT
Requires Python: >=3.7,<4.0

# pgcr_mongo

## 介绍
python操作MongoDB数据库的简单工具，支持异步，可以结合FastAPI使用

## 快速入门

### 安装
```shell
pip install pgcr_mongo
```

### 建立连接

```shell
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017", "pygosuperman")
print(mongo_db)
```

### 判断数据库是否已存在

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")
print(mongo_db.exists("pygosuperman"))
print(mongo_db.exists("pygosuperman111"))
```

### 创建新的集合

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")
col = mongo_db.new_collection("col")
print(col)
```

### 向集合中插入文档

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")
person = {"name": "zhangsan", "age": 33}
id = mongo_db.add("persons", person)
print(id)
```

### 向集合中插入多条文档

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")
mylist = [
    {"name": "Taobao", "alexa": "100", "url": "https://www.taobao.com"},
    {"name": "QQ", "alexa": "101", "url": "https://www.qq.com"},
    {"name": "Facebook", "alexa": "10", "url": "https://www.facebook.com"},
    {"name": "知乎", "alexa": "103", "url": "https://www.zhihu.com"},
    {"name": "Github", "alexa": "109", "url": "https://www.github.com"}
]
ids = mongo_db.add("persons", mylist, True)
print(ids)
```

### 修改数据
注意：只能用“$set”来修改数据
```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")

mongo_db.update("persons", {"name": "QQ"}, {"$set": {"alexa": 111}}, many=True)
result = mongo_db.find("persons", {"name": {"$regex": "^Q"}})
for p in result:
    print(p)
```

### 删除数据
```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")

result = mongo_db.delete("persons", {"name": {"$regex": "^G"}})
```

### 查询所有

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")

# 查询所有
result = mongo_db.find("persons", many=True)
print(result)
for p in result:
    print(p)
```

### 查询单个

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")

result = mongo_db.find("persons", many=False)
print(result)
```

### 等值查询

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")

result = mongo_db.find("persons", {"name": "Taobao"})
print(result)
for p in result:
    print(p)
```

### 条件查询

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")

result = mongo_db.find("persons", {"alexa": {"$gte": "0"}})
print(result)
for p in result:
    print("=====", p)
```

### 正则表达式查询

```python
from pgcr_mongo import register_mongo_db

mongo_db = register_mongo_db("localhost", "27017")

result = mongo_db.find("persons", {"name": {"$regex": "^G"}})
print(result)
for p in result:
    print("=====", p)
```

## 异步操作
### 建立连接
```python
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017", "pygosuperman")
print(mongo_db)
```

### 判断数据库是否存在
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.exists("pygosuperman")
    print("执行结果： ", result)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 判断集合是否存在
```shell
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.exists_collection("persons")
    print("执行结果： ", result)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 添加一条数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.add("persons", {"aaa": "bbb"})
    print("执行结果： ", result)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 添加多条数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.add("persons", [{"aaa": "bbb"} for i in range(10)], many=True)
    print("执行结果： ", result)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 修改一条数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.update("persons", {"aaa": "bbb"}, {"$set": {"aaa": "abc"}})
    print("执行结果： ", result)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 修改多条数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.update("persons", {"aaa": "bbb"}, {"$set": {"aaa": "abc"}}, many=True)
    print("执行结果： ", result)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 删除多条数据
目前只支持删除多条数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.delete("persons", {"aaa": "abc"})
    print("执行结果： ", result)
    # for i in result:
    #     print(i)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 查询单条数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.find("persons", {"aaa": "abc"}, many=False)
    print("执行结果： ", result)
    # for i in result:
    #     print(i)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 查询多条数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.find("persons", {"aaa": "abc"})
    # print("执行结果： ", result)
    for i in result:
        print(i)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 统计表数据
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.count("persons", {})
    print("执行结果： ", result)
    # for i in result:
    #     print(i)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```

### 排序和分页
```python
import asyncio
from pgcr_mongo import register_async_mongo_db

mongo_db = register_async_mongo_db("localhost", "27017")


async def test():
    result = await mongo_db.page("persons", "_id", {}, 2, 2)
    print("执行结果： ", result)


loop = asyncio.get_event_loop()
loop.run_until_complete(test())
```