Last active
January 11, 2019 21:12
-
-
Save michaelbukachi/fa1107eb31d6c233e944dc7ba7c3a553 to your computer and use it in GitHub Desktop.
Mongoengine case insensitive query with _in operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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