8000 feat: support in aggregation pipeline by LoicAvelin · Pull Request #926 · mongomock/mongomock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: support in aggregation pipeline #926

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
8000
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
26 changes: 25 additions & 1 deletion mongomock/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@
return self._handle_type_operator(k, v)
if k in boolean_operators:
return self._handle_boolean_operator(k, v)
if k in text_search_operators + projection_operators + object_operators:
if k in object_operators:
return self._handle_object_operator(k, v)

Check warning on line 279 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L279

Added line #L279 was not covered by tests
if k in text_search_operators + projection_operators:
raise NotImplementedError(
f"'{k}' is a valid operation but it is not supported by Mongomock yet."
)
Expand All @@ -301,6 +303,28 @@
else:
raise

def _handle_object_operator(self, operator, expression):
if operator == '$mergeObjects':
return self._merge_objects(expression)

Check warning on line 308 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L307-L308

Added lines #L307 - L308 were not covered by tests

def _merge_objects(self, docs):
"""
Merges a list of dictionaries into a single dictionary.
Ignores values that are not dictionaries.
"""
merged = {}

Check warning on line 315 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L315

Added line #L315 was not covered by tests
# Parse the expression to resolve any nested operations
docs = self.parse(docs) if not isinstance(docs, list) else docs

Check warning on line 317 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L317

Added line #L317 was not covered by tests

for doc in docs:

Check warning on line 319 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L319

Added line #L319 was not covered by tests
# Handle all value types
parsed = self.parse(doc) if isinstance(doc, dict) else doc

Check warning on line 321 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L321

Added line #L321 was not covered by tests

if isinstance(parsed, dict):
merged.update(parsed)

Check warning on line 324 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L323-L324

Added lines #L323 - L324 were not covered by tests

return merged

Check warning on line 326 in mongomock/aggregate.py

View check run for this annotation

Codecov / codecov/patch

mongomock/aggregate.py#L326

Added line #L326 was not covered by tests

def _parse_to_bool(self, expression):
"""Parse a MongoDB expression and then convert it to bool"""
# handles converting `undefined` (in form of KeyError) to False
Expand Down
6 changes: 6 additions & 0 deletions tests/test__mongomock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4704,6 +4704,12 @@ def test__aggregate_merge_objects(self):
{'_id': ObjectId(), 'a': 3, 'b': None},
{'_id': ObjectId(), 'a': 3, 'b': {}},
{'_id': ObjectId(), 'a': 4, 'b': None},
{'_id': ObjectId(), 'a': 'multi', 'b': {'x': 1}},
{'_id': ObjectId(), 'a': 'multi', 'b': {'y': 2}},
{'_id': ObjectId(), 'a': 'multi', 'b': {'z': 3}},
{'a': 'non_dict', 'b': {'note': 'was a string'}},
{'_id': ObjectId(), 'a': 'non_dict', 'b': {'valid': True}},
{'_id': ObjectId(), 'a': 'empty', 'b': {}}
]
)
pipeline = [
Expand Down
Loading
0