Request multiple values ​​at a time pymongo

Currently, I have a mongo document that looks like this:

{'_id': id, 'title': title, 'date': date}

What I'm trying to search by name in this document, in the database I have 5 items that are not many, but my file has 1 million titles to search.

I guarantee the name as an index in the collection, but still the running time is rather slow (about 40 seconds per 1000 names, something obvious, since I am making a request for the title), here is my code:

Creating a job repository:

class WorkRepository(GenericRepository, Repository):
    def __init__(self, url_root):
        super(WorkRepository, self).__init__(url_root, 'works')
        self._db[self.collection].ensure_index('title')

Program input (this is REST api):

start = time.clock()
for work in json_works: #1000 titles per request
    result = work_repository.find_works_by_title(work['title'])

    if result:
        works[work['id']] = result

end = time.clock()
print end-start

return json_encoder(request, works)

and find_works_by_title:

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

    return works

I am new to mongo and probably I made some kind of mistake, any recommendation?

+4
source share
1 answer

. ( ).

( , ):

# 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}})

$in: http://docs.mongodb.org/manual/reference/operator/query/in/

, explain find ( : http://docs.mongodb.org/manual/reference/method/cursor.explain/) , , , . , , .

+5

All Articles