MongoDB using an OR clause in mongoengine
The mongoengine documentation is apparently incorrect in this case. Instead of using the bitwise operators "&" and "|", you should use the standard operators "and" and "or".
So your first query becomes:
query = ContentItem.objects.filter( (Q(account=account) and Q(public=True)) or (Q(account=account) and Q(creator=logged_in_user)) ).order_by('-last_used')
The correct way to do the query is to use bitwise operations |
and &
the way you wrote it in your question:
query = ContentItem.objects.filter( (Q(account=account) & Q(public=True)) | (Q(account=account) & Q(creator=logged_in_user)) ).order_by('-last_used')
Note: using the standard Python boolean operators and
and or
will not work. This is explained in the MongoEngine documentation.