Skip to content

Loading…

Use query to filter by keys when grouping #34

Merged
merged 1 commit into from

2 participants

@robyoung
Government Digital Service member

Rather than filtering out results that have null values for keys
after the group handle it in the condition. This let's Mongo do the hard
work so we don't have to.

I completely forgot about this when I merged the pull request earlier.

@robyoung robyoung Use query to filter by keys when grouping
Rather than filtering out results that have null values for keys
after the group handle it in the condition. This let's Mongo do the hard
work so we don't have to.
b476245
@pbadenski pbadenski merged commit aa0457b into master

1 check passed

Details default Build #132 succeeded on Jenkins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 3, 2013
  1. @robyoung

    Use query to filter by keys when grouping

    robyoung committed
    Rather than filtering out results that have null values for keys
    after the group handle it in the condition. This let's Mongo do the hard
    work so we don't have to.
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 6 deletions.
  1. +9 −6 backdrop/core/database.py
View
15 backdrop/core/database.py
@@ -102,20 +102,23 @@ def build_reducer(self, collect):
reducer = Code(reducer_code)
return (initial, reducer)
+ def _require_keys_in_query(self, keys, query):
+ for key in keys:
+ if key not in query:
+ query[key] = {}
+ query[key]['$ne'] = None
+ return query
+
def _group(self, keys, query, sort=None, limit=None, collect=None):
initial, reducer = self.build_reducer(collect or [])
+
results = self._collection.group(
key=keys,
- condition=query,
+ condition=self._require_keys_in_query(keys, query),
initial=initial,
reduce=reducer
)
- results = [
- result for result in results
- if all(result[key] is not None for key in keys)
- ]
-
results = nested_merge(keys, collect, results)
if sort:
Something went wrong with that request. Please try again.