Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pymongo一次查询多个值_Python_Mongodb_Pymongo - Fatal编程技术网

Python pymongo一次查询多个值

Python pymongo一次查询多个值,python,mongodb,pymongo,Python,Mongodb,Pymongo,目前,我有一个mongo文档,如下所示: {'_id': id, 'title': title, 'date': date} 我尝试的是在这个文档中按标题进行搜索,在数据库中我有5公里的项目,这并不多,但我的文件有100万个标题要搜索 我已确保将标题作为集合中的索引,但性能时间仍然很慢(每1000个标题大约40秒,这一点很明显,因为我正在对每个标题进行查询),以下是我到目前为止的代码: 工作存储库创建: class WorkRepository(GenericRepository, Repos

目前,我有一个mongo文档,如下所示:

{'_id': id, 'title': title, 'date': date}
我尝试的是在这个文档中按标题进行搜索,在数据库中我有5公里的项目,这并不多,但我的文件有100万个标题要搜索

我已确保将标题作为集合中的索引,但性能时间仍然很慢(每1000个标题大约40秒,这一点很明显,因为我正在对每个标题进行查询),以下是我到目前为止的代码:

工作存储库创建:

class WorkRepository(GenericRepository, Repository):
    def __init__(self, url_root):
        super(WorkRepository, self).__init__(url_root, 'works')
        self._db[self.collection].ensure_index('title')
程序条目(是REST api):

并通过标题代码查找作品:

def find_works_by_title(self, work_title):
    works = list(self._db[self.collection].find({'title': work_title}))

    return works

我是mongo的新手,可能我犯了一些错误,有什么建议吗?

您正在为您的每一个标题向DB打一个电话。往返将大大降低进程的速度(程序和数据库将花费大部分时间进行网络通信,而不是实际工作)

尝试以下方法(当然,根据您的程序结构进行调整):

有关运算符中的
$in如何工作的进一步参考:


如果在此之后您的查询仍然很慢,请在
查找
调用的返回值(此处的更多信息:)上使用
解释
,并检查查询是否实际使用了索引。如果不是,请找出原因。

将尝试,但看起来正是我想要的:)当使用in运算符时,性能当然会提高,但是我没有得到任何匹配(我在每个标题序列中添加一个已知值以确保某些内容匹配),但直到现在才算幸运。好吧,我的错,使用了多个[[在生成列表时,它工作得非常好。
def find_works_by_title(self, work_title):
    works = list(self._db[self.collection].find({'title': work_title}))

    return works
# Build a list of the 1000 titles you're searching for.
titles = [w["title"] for w in json_works]

# Make exactly one call to the DB, asking for all of the matching documents.
return collection.find({"title": {"$in": titles}})