Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class DatabaseFeatures(GISFeatures, BaseDatabaseFeatures):
"model_fields.test_jsonfield.TestQuerying.test_icontains",
# Unexpected alias_refcount in alias_map.
"queries.tests.Queries1Tests.test_order_by_tables",
# Pattern lookups (startswith, regex, etc.) don't work on non-string
# fields: https://jira.mongodb.org/browse/INTPYTHON-734
"admin_changelist.tests.ChangeListTests.test_pk_in_search_fields",
"admin_changelist.tests.ChangeListTests.test_related_field_multiple_search_terms",
"lookup.tests.LookupTests.test_lookup_int_as_str",
"lookup.tests.LookupTests.test_regex_non_string",
# The $sum aggregation returns 0 instead of None for null.
"aggregation.test_filter_argument.FilteredAggregateTests.test_plain_annotate",
"aggregation.tests.AggregateTestCase.test_aggregation_default_passed_another_aggregate",
Expand Down
2 changes: 1 addition & 1 deletion django_mongodb_backend/query_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ def process_rhs(node, compiler, connection):
def regex_match(field, regex_vals, insensitive=False):
regex = {"$concat": regex_vals} if isinstance(regex_vals, tuple) else regex_vals
options = "i" if insensitive else ""
return {"$regexMatch": {"input": {"$toString": field}, "regex": regex, "options": options}}
return {"$regexMatch": {"input": field, "regex": regex, "options": options}}
14 changes: 14 additions & 0 deletions docs/source/releases/5.2.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
Django MongoDB Backend 5.2.x
============================

5.2.0 beta 3
============

*Unreleased*

Backwards incompatible changes
------------------------------

- Pattern matching lookups (``iexact``, ``startswith``, ``istartswith``,
``endswith``, ``iendswith``, ``contains``, ``icontains``, ``regex``,
and ``iregex``) no longer support non-string fields. These lookups previously
cast their input using ``$toString`` but this caused some queries to perform
poorly because MongoDB couldn't use indexes when running the query.

5.2.0 beta 2
============

Expand Down
5 changes: 5 additions & 0 deletions docs/source/topics/known-issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Querying
- You can study the skipped tests in ``DatabaseFeatures.django_test_skips``
for more details on known issues.

- Pattern matching lookups (:lookup:`iexact`, :lookup:`startswith`,
:lookup:`istartswith`, :lookup:`endswith`, :lookup:`iendswith`,
:lookup:`contains`, :lookup:`icontains`, :lookup:`regex`,
and :lookup:`iregex`) don't support non-string fields.

Database functions
==================

Expand Down
7 changes: 7 additions & 0 deletions tests/lookup_/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from django.db import models


class Book(models.Model):
title = models.CharField(max_length=10)

def __str__(self):
return self.title


class Number(models.Model):
num = models.IntegerField(blank=True, null=True)

Expand Down
17 changes: 16 additions & 1 deletion tests/lookup_/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase

from .models import Number
from .models import Book, Number


class NumericLookupTests(TestCase):
Expand All @@ -15,3 +15,18 @@ def test_lt(self):

def test_lte(self):
self.assertQuerySetEqual(Number.objects.filter(num__lte=3), self.objs[:4])


class RegexTests(TestCase):
def test_mql(self):
# $regexMatch must not cast the input to string, otherwise MongoDB
# can't use the field's indexes.
with self.assertNumQueries(1) as ctx:
list(Book.objects.filter(title__regex="Moby Dick"))
query = ctx.captured_queries[0]["sql"]
self.assertEqual(
query,
"db.lookup__book.aggregate(["
"{'$match': {'$expr': {'$regexMatch': {'input': '$title', "
"'regex': 'Moby Dick', 'options': ''}}}}])",
)
Loading