0

我正在将上次保存的数据检索到文本字段,以便用户可以继续他们之前所做的工作,此命令在 mongo 控制台中运行良好,在 PyCharm 中我无法找出编写 find() 查询的正确方法

我尝试按照此处的建议将过滤器参数存储在 2 个单独的变量中pymongo error: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping

Database.initialize()
db_data = Database.find(collection = 'notes_temp', 
                         query = ({"priority" : self.priority}, {'td': 1, '_id':0}))
        for task in db_data:
            self.t.insert('insert linestart', task)

数据库模块

class Database:
    URI = "mongodb://127.0.0.1:27017"
    DATABASE = None

    @staticmethod
    def initialize():
        client = pymongo.MongoClient(Database.URI)
        Database.DATABASE = client['notes']
    @staticmethod
    def find(collection, query):
        return Database.DATABASE[collection].find(query)
4

1 回答 1

1

问题是您的查询定义为:

({"priority" : self.priority}, {'td': 1, '_id':0}))

因此,当您的函数将其作为输入时,其解析为:

return Database.DATABASE[collection].find(({"priority" : self.priority}, {'td': 1, '_id':0})))

基本上有一对额外的括号应该被删除。

一个模拟解决方案:(显然有几种方法可以处理这个问题)

Database.initialize()
db_data = Database.find(collection = 'notes_temp', 
                         query = {"priority" : self.priority}, projection = {'td': 1, '_id':0})
        for task in db_data:
            self.t.insert('insert linestart', task)

class Database:
    URI = "mongodb://127.0.0.1:27017"
    DATABASE = None

    @staticmethod
    def initialize():
        client = pymongo.MongoClient(Database.URI)
        Database.DATABASE = client['notes']
    @staticmethod
    def find(collection, query, projection):
        return Database.DATABASE[collection].find(query, projection)
于 2019-04-22T11:16:51.387 回答