Skip to content
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

add ArrayField #197

Merged
merged 5 commits into from
Jan 9, 2025
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ repos:
hooks:
- id: rstcheck
additional_dependencies: [sphinx]
args: ["--ignore-directives=fieldlookup,setting", "--ignore-roles=lookup,setting"]

# We use the Python version instead of the original version which seems to require Docker
# https://github.com/koalaman/shellcheck-precommit
Expand Down
54 changes: 13 additions & 41 deletions django_mongodb_backend/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def order_by(self, compiler, connection):
return self.expression.as_mql(compiler, connection)


def query(self, compiler, connection, lookup_name=None):
def query(self, compiler, connection, get_wrapping_pipeline=None):
subquery_compiler = self.get_compiler(connection=connection)
subquery_compiler.pre_sql_setup(with_col_aliases=False)
field_name, expr = subquery_compiler.columns[0]
Expand All @@ -119,48 +119,20 @@ def query(self, compiler, connection, lookup_name=None):
for col, i in subquery_compiler.column_indices.items()
},
}
# The result must be a list of values. The output is compressed with an
# aggregation pipeline.
if lookup_name in ("in", "range"):
if subquery.aggregation_pipeline is None:
subquery.aggregation_pipeline = []
wrapping_result_pipeline = [
{
"$facet": {
"group": [
{
"$group": {
"_id": None,
"tmp_name": {
"$addToSet": expr.as_mql(subquery_compiler, connection)
},
}
}
]
}
},
{
"$project": {
field_name: {
"$ifNull": [
{
"$getField": {
"input": {"$arrayElemAt": ["$group", 0]},
"field": "tmp_name",
}
},
[],
]
}
}
},
]
if get_wrapping_pipeline:
# The results from some lookups must be converted to a list of values.
# The output is compressed with an aggregation pipeline.
wrapping_result_pipeline = get_wrapping_pipeline(
subquery_compiler, connection, field_name, expr
)
# If the subquery is a combinator, wrap the result at the end of the
# combinator pipeline...
if subquery.query.combinator:
subquery.combinator_pipeline.extend(wrapping_result_pipeline)
# ... otherwise put at the end of subquery's pipeline.
else:
if subquery.aggregation_pipeline is None:
subquery.aggregation_pipeline = []
subquery.aggregation_pipeline.extend(wrapping_result_pipeline)
# Erase project_fields since the required value is projected above.
subquery.project_fields = None
Expand All @@ -185,13 +157,13 @@ def star(self, compiler, connection): # noqa: ARG001
return {"$literal": True}


def subquery(self, compiler, connection, lookup_name=None):
return self.query.as_mql(compiler, connection, lookup_name=lookup_name)
def subquery(self, compiler, connection, get_wrapping_pipeline=None):
return self.query.as_mql(compiler, connection, get_wrapping_pipeline=get_wrapping_pipeline)


def exists(self, compiler, connection, lookup_name=None):
def exists(self, compiler, connection, get_wrapping_pipeline=None):
try:
lhs_mql = subquery(self, compiler, connection, lookup_name=lookup_name)
lhs_mql = subquery(self, compiler, connection, get_wrapping_pipeline=get_wrapping_pipeline)
except EmptyResultSet:
return Value(False).as_mql(compiler, connection)
return connection.mongo_operators["isnull"](lhs_mql, False)
Expand Down
7 changes: 7 additions & 0 deletions django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"auth_tests.test_views.LoginTest.test_login_session_without_hash_session_key",
# GenericRelation.value_to_string() assumes integer pk.
"contenttypes_tests.test_fields.GenericRelationTests.test_value_to_string",
# icontains doesn't work on ArrayField:
# Unsupported conversion from array to string in $convert
"model_fields_.test_arrayfield.QueryingTests.test_icontains",
# ArrayField's contained_by lookup crashes with Exists: "both operands "
# of $setIsSubset must be arrays. Second argument is of type: null"
# https://jira.mongodb.org/browse/SERVER-99186
"model_fields_.test_arrayfield.QueryingTests.test_contained_by_subquery",
}
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
_django_test_expected_failures_bitwise = {
Expand Down
3 changes: 2 additions & 1 deletion django_mongodb_backend/fields/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .array import ArrayField
from .auto import ObjectIdAutoField
from .duration import register_duration_field
from .json import register_json_field
from .objectid import ObjectIdField

__all__ = ["register_fields", "ObjectIdAutoField", "ObjectIdField"]
__all__ = ["register_fields", "ArrayField", "ObjectIdAutoField", "ObjectIdField"]


def register_fields():
Expand Down
Loading
Loading