Python can';t在pymongo中的集合实例中使用find()
我有一门课:Python can';t在pymongo中的集合实例中使用find(),python,pymongo,Python,Pymongo,我有一门课: 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__, '
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)
我创建的类实例如下所示:
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
emails = collections_dict['email_logs']
db\u集合\u名称
包含电子邮件日志
。我创建了一个电子邮件
实例,如下所示:
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
emails = collections_dict['email_logs']
打印电子邮件
产生
打印电子邮件。find()
产生:
Traceback (most recent call last):
File "main.py", line 75, in <module>
program.runProgram()
File "main.py", line 63, in runProgram
print emails.find
AttributeError: Collection instance has no attribute 'find'
回溯(最近一次呼叫最后一次):
文件“main.py”,第75行,在
program.runProgram()
runProgram中第63行的文件“main.py”
打印电子邮件。查找
AttributeError:集合实例没有属性“find”
为什么会出错?不知道为什么要费心重新定义Pymongo集合类
from pymongo import MongoClient
db = MongoClient().my_database # create an instance of pymongo Database
emails = db.emails # create an instance of pymongo Collection
emails.find() # yeilds a pymongo Cursor (containing the query results)
我在
find()
方法之前缺少集合
emails.collection.find()