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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 38 additions & 2 deletions django_mongodb_backend/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ def query(self, compiler, connection, lookup_name=None):
for col, i in subquery_compiler.column_indices.items()
},
}
wrapping_result_pipeline = None
# 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": {
Expand Down Expand Up @@ -155,12 +154,49 @@ def query(self, compiler, connection, lookup_name=None):
}
},
]
if lookup_name == "overlap":
wrapping_result_pipeline = [
{
"$facet": {
"group": [
{"$project": {"tmp_name": expr.as_mql(subquery_compiler, connection)}},
{
"$unwind": "$tmp_name",
},
{
"$group": {
"_id": None,
"tmp_name": {"$addToSet": "$tmp_name"},
}
},
]
}
},
{
"$project": {
field_name: {
"$ifNull": [
{
"$getField": {
"input": {"$arrayElemAt": ["$group", 0]},
"field": "tmp_name",
}
},
[],
]
}
}
},
]
if wrapping_result_pipeline:
# 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 Down
3 changes: 3 additions & 0 deletions django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ 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",
}
# $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