title: Python - Mongoengine advanced query list field
body: <p>I have a list of lists, like so:</p>
  
  <pre><code>li = [[('A', 'one'), ('A', 'two')], [('B', 'three'), ('B', 'four')]]
  </code></pre>
  
  <p>and I need to query a mongo database to get all objects whose list field contains at least ONE item in each sublist of <code>li</code>.  Ex: items that have either [('A', 'one') OR ('A', 'two')] AND either [('B', 'three') OR ('B', 'four')] ...</p>
  
  <p>I am using mongoengine, but that may be able to change if I can use something else to get this done.  So right now I am doing many queries like so, to avoid getting duplicate entries:</p>
  
  <pre><code>final = set()
  for sublist in li:
      query = Obj.objects(list_field__in=sublist)
      final &amp;= set(query)
  </code></pre>
  
  <p>The problem is that this is very slow when dealing with large query results (making the set takes a very long time, I believe).  Is there a way that I can speed this up?  Specifically, is there a way that I can avoid set/list creation from the query results?</p>
  
  <p>I would really like to be able to somehow write something like this:</p>
  
  <pre><code>query = Obj.objects(list_field__in=li[0] AND list_field__in=li[1] AND ...)
  </code></pre>
  
  <p>Edit: The answer below does not work upon further testing, because mongoengine does not allow <code>Q(field=x) &amp; Q(field=y)</code></p>
  
  <p>Edit2: Here is the equivalent mongoDB query that I want to do:</p>
  
  <pre><code>db.obj.find({ "$and": [
      {"list_field": {"$in":
          [["A", "one"], ["A", "two"]]
      }},
      {"list_field": {"$in":
          [["B", "three"], ["B", "four"]]
      }}
  ]})
  </code></pre>
  
  <p>Can I do this in mongoengine?  It will not let me do a query with <code>Q(list_field__in=[('A', 'one'), ('A', 'two')]) | Q(list_field__in=[('B', 'three'), ('B', 'four')])</code></p>