Skip to content

Instantly share code, notes, and snippets.

@michaelbukachi
Last active January 11, 2019 21:12
Show Gist options
  • Save michaelbukachi/fa1107eb31d6c233e944dc7ba7c3a553 to your computer and use it in GitHub Desktop.
Save michaelbukachi/fa1107eb31d6c233e944dc7ba7c3a553 to your computer and use it in GitHub Desktop.
Mongoengine case insensitive query with _in operator
import operator
from functools import reduce
from mongoengine import Document, Q, StringField
class Person(Document):
name = StringField()
names = ['mike', 'John', 'JIMMY']
people = Person.objects(name__in=names) # Will only match the exact case
query = reduce(operator.or_, [Q(name__iexact=name) for name in names])
people = Person.objects(query) # Will ignore case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment