I have a list of lists, for example:
li = [[('A', 'one'), ('A', 'two')], [('B', 'three'), ('B', 'four')]]
and I need to query the mongo database to get all the objects whose list box contains at least one element in each li sublist. Ex: elements that have either [('A', 'one') OR ('A', 'two')] and either [('B', 'three') OR ('B', 'four') ] ...
I use mongoengine, but that can change if I can use something else to do this. So now I am making a lot of queries to avoid duplicate entries:
final = set() for sublist in li: query = Obj.objects(list_field__in=sublist) final &= set(query)
The problem is that it is very slow when working with large query results (which makes the set take a very long time, I think). Is there any way to speed this up? In particular, is there a way to avoid creating / creating a list from query results?
I would really like to somehow write something like this:
query = Obj.objects(list_field__in=li[0] AND list_field__in=li[1] AND ...)
Edit: the answer below does not work for further testing because mongoengine does not allow Q(field=x) & Q(field=y)
Edit2: Here is the equivalent mongoDB request I want to do:
db.obj.find({ "$and": [ {"list_field": {"$in": [["A", "one"], ["A", "two"]] }}, {"list_field": {"$in": [["B", "three"], ["B", "four"]] }} ]})
Can I do it in mongoengine? This will not allow me to make a request with Q(list_field__in=[('A', 'one'), ('A', 'two')]) | Q(list_field__in=[('B', 'three'), ('B', 'four')]) Q(list_field__in=[('A', 'one'), ('A', 'two')]) | Q(list_field__in=[('B', 'three'), ('B', 'four')])