mongo_api.py 918 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2023/7/21 8:51
  3. # @Author : XuJiakai
  4. # @File : mongo_api
  5. # @Software: PyCharm
  6. import asyncio
  7. from data_clean.utils.async_client import get_aio_mongo_db
  8. db = get_aio_mongo_db()
  9. async def insert_one(collection, data):
  10. result = await db[collection].insert_one(data)
  11. return result.inserted_id
  12. pass
  13. async def insert_many(collection, data: list):
  14. result = await db[collection].insert_many(data, ordered=False)
  15. return result.inserted_id
  16. pass
  17. async def delete_one(collection, object_id):
  18. result = await db[collection].delete_one({"_id": object_id})
  19. return result.deleted_count
  20. pass
  21. async def test():
  22. # res = await db["a_xjk_test_aio"].insert_one({"a": "b"})
  23. # print(res.inserted_id)
  24. res = await insert_one("a_xjk_test_aio", {"_id": "b", "c": "a"})
  25. print(res)
  26. pass
  27. if __name__ == '__main__':
  28. asyncio.run(test())
  29. pass