MongoDB find()返回空数组

2024-09-30 01:29:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的烧瓶应用程序,可以执行积垢操作。我有一个python模块,我在其中定义了所有函数,它们都工作得很好;我可以创建、读取、更新和删除。我添加了一个检索方法,在这里我调用find(),它确实检索光标。问题是我无法让它填充数据,我得到的只是一个空数组[]。我循环遍历游标,使用了bson.json_util的加载和转储,但得到了相同的结果。下面是该类的外观:

from pymongo import MongoClient

class AnimalShelter(object):

    def __init__(self, username, password):

        # Access the MongoDB databases and collections.
        self.client = MongoClient('localhost', ####)
        self.db = self.client['project']
        self.username = username
        self.password = password


# Create
    def create(self, data):
        if data is not None:
            return self.db.collection.insert(data) 
        else:
            print("Nothing to save, because data parameter is empty")
            return False
            
# Read
...
# Update
...
# Delete
...


# Retrieve
    def retrieve(self, data, projection):
        if data is not None:
            return self.db.collection.find(data, projection)
        else:
            return False

在测试文件中,我有:

animals = AnimalShelter(username, password)
            try:
                dog = dumps(list(animals.retrieve({"animal_type": "Dog", "name": "Flow"}, {"_id": False})))
                print(dog)

            except:
                print("Unable to find the outcome")

任何帮助都将不胜感激


Tags: theselfclientfalsedbdatareturnis

热门问题