作者:依心摇曳少校 | 来源:互联网 | 2023-05-17 02:30
Ihaveaclass:我有一节课:importsysimportosimportpymongofrompymongoimportMongoClientclassC
I have a class:
我有一节课:
import sys
import os
import pymongo
from pymongo import MongoClient
class Collection():
def __init__(self, db, collection_name):
self.db = db
self.collection_name = collection_name
if not hasattr(self.__class__, 'client'):
self.__class__.client = MongoClient()
self.data_base = getattr(self.client, self.db)
self.collection = getattr(self.data_base, self.collection_name)
I created class instances as follows:
我创建了类实例,如下所示:
def getCollections(self):
collections_dict = {}
for i in range(len(self.db_collection_names)):
collections_dict[self.db_collection_names[i]] = Collection(self.database_name, self.db_collection_names[i])
return collections_dict
db_collection_names
contains email_logs
. I created an emails
instance as follows:
db_collection_names包含email_logs。我创建了一个电子邮件实例,如下所示:
emails = collections_dict['email_logs']
print emails
yields
打印电子邮件会产生
print emails.find()
yields:
print emails.find()产生:
Traceback (most recent call last):
File "main.py", line 75, in
program.runProgram()
File "main.py", line 63, in runProgram
print emails.find
AttributeError: Collection instance has no attribute 'find'
Why the error?
为什么错误?
2 个解决方案