| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- from db import MongoClientHelper
- class MongoDao:
- _instance = None
-
- def __new__(cls, collection_name):
- if not cls._instance:
- cls._instance = super(MongoDao, cls).__new__(cls)
- cls._instance._initialized = False
- return cls._instance
-
- def __init__(self, collection_name):
- if not self._initialized:
- self.db_client = MongoClientHelper()
- self._initialized = True
- self.collection_name = collection_name
-
- def get_one_record_by_query(self, query):
- res = self.db_client.find_one(self.collection_name, query)
- return res
-
- def get_records_by_query(self, query):
- collections = self.db_client.find_many(self.collection_name, query)
- records = [collection for collection in collections]
- return records
-
- def get_one_field_data(self, field):
- fields = [field]
- """获取指定key的所有数据,返回列表"""
- field_records = self.db_client.find_fields(self.collection_name, fields)
- return [record[field] for record in field_records]
-
- def get_fields_data(self, fields):
- records = self.db_client.find_fields(self.collection_name, fields)
- return [record for record in records]
-
- def get_all_records(self):
- records = self.db_client.find_all(self.collection_name)
- return [record for record in records]
-
- if __name__ == '__main__':
- collection_name = "obrand-ec"
- dao = MongoDao(collection_name)
- field = "nick"
- res = dao.get_all_records()
- print(len(res))
|