Skip to content

remove unnecessary $convert in ObjectIdAutoField $lookup queries #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2024
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
3 changes: 3 additions & 0 deletions django_mongodb/fields/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def get_prep_value(self, value):
def db_type(self, connection):
return "objectId"

def rel_db_type(self, connection):
return "objectId"

def to_python(self, value):
if value is None or isinstance(value, int):
return value
Expand Down
Empty file added tests/queries_/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions tests/queries_/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import models


class Author(models.Model):
name = models.CharField(max_length=10)

def __str__(self):
return self.name


class Book(models.Model):
title = models.CharField(max_length=10)
author = models.ForeignKey(Author, models.CASCADE)

def __str__(self):
return self.title
26 changes: 26 additions & 0 deletions tests/queries_/test_mql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.test import TestCase

from .models import Author, Book


class MQLTests(TestCase):
def test_all(self):
with self.assertNumQueries(1) as ctx:
list(Author.objects.all())
query = ctx.captured_queries[0]["sql"]
self.assertEqual(query, "db.queries__author.aggregate([{'$match': {'$expr': {}}}])")

def test_join(self):
with self.assertNumQueries(1) as ctx:
list(Book.objects.filter(author__name="Bob"))
query = ctx.captured_queries[0]["sql"]
self.assertEqual(
query,
"db.queries__book.aggregate(["
"{'$lookup': {'from': 'queries__author', "
"'let': {'parent__field__0': '$author_id'}, "
"'pipeline': [{'$match': {'$expr': "
"{'$and': [{'$eq': ['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, "
"{'$unwind': '$queries__author'}, "
"{'$match': {'$expr': {'$eq': ['$queries__author.name', 'Bob']}}}])",
)