-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: django
-
None
In order to support pattern lookups (startswith, endswith, regex, etc.) on non-string fields, we cast the input using $toString. jib.adegunloye@mongodb.com reported that this causes poor performance on queries since the $toString prevents MongoDB from being able to uses indexes on the field. We'll remove this cast for now and try to add support for pattern lookups on non-string fields in the future (it's unclear how it could be done).
class Author(models.Model):
  name = models.CharField(...)
class Book(models.Model):
  author = models.ForeignKey(Author)
>>> exp = json.loads(Book.objects.filter(author__name__icontains="0").explain())
>>> pprint.pprint(exp['command']['pipeline'])
[{'$lookup': {'as': 'polls_author',
       'from': 'polls_author',
       'let': {'parent__field__0': '$author_id'},
       'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$$parent__field__0', '$_id']},
                             {'$regexMatch': {'input': {'$toString': '$name'},
                                      'options': 'i',
                                      'regex': '0'}}]}}}]}},
 {'$unwind': '$polls_author'},
 # Everything after the $unwind is redundant
 {'$match': {'$expr': {'$regexMatch': {'input': {'$toString': '$polls_author.name'},
                    'options': 'i',
                    'regex': '0'}}}}]