Answer the question
In order to leave comments, you need to log in
How to work with a selection from Mongodb in order to avoid unnecessary calls to the database?
Good morning.
Situation: I get a number of records from Mongodb according to the parameters I need, for example:
tasks_query = task_base.find({
"$and": [{
"order_id": x
}, {
"site": y
}, {
"category": z
}]
})
for task in task_query:
if task['order_id'] = 234:
return task['category']
Answer the question
In order to leave comments, you need to log in
Mongo is incredibly fast to read. Let it be she who is engaged in searching and sorting, and not python; that's what it's designed for. Don't be afraid to load it with requests.
Finally, make a very simple test. Take the time and describe the required functionality in both versions: monga and python. Measure running time. And everything will fall into place.
Then I need to go through the cycles several thousand times ...It makes no sense, because the result will be only one order_id.
When converted to a dictionary, it will be + - the sameOne dictionary - one index, if you need several indexes - make several dictionaries. This is the fastest - O(1) if everything fits into memory. You don't need a special person for this.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question