Commit cab81724 by Peter Weidenbach

docstrings added; match test added

parent cab4cfa7
......@@ -3,6 +3,20 @@ from bson.son import SON
def get_objects_and_count_of_occurrence(collection, object_path, unwind=False, match=None):
"""
Get a list of unique values and their occurences on a specific object path in a collection.
An Optional search string (match) can be added.
:param collection: mongo collection to look at
:type collection: pymongo.collection
:param object_path: mongo object path
:type object_path: str
:param unwind: if true: handle list entries as single values
:type unwind: bool
:param match: mongo search string
:type match: dict
:return: [{'_id': <VALUE>, 'count': <OCCURENCES>}, ...]
"""
pipeline = []
if match is not None:
pipeline.append({"$match": match})
......@@ -20,10 +34,34 @@ def get_objects_and_count_of_occurrence(collection, object_path, unwind=False, m
def get_field_sum(collection, object_path, match=None):
"""
Get sum of all values in this field
An Optional search string (match) can be added.
:param collection: mongo collection to look at
:type collection: pymongo.collection
:param object_path: mongo object path
:type object_path: str
:param match: mongo search string
:type match: dict
:return: int
"""
return get_field_execute_operation("$sum", collection, object_path, match=match)
def get_field_average(collection, object_path, match=None):
"""
Get average of all values in this field
An Optional search string (match) can be added.
:param collection: mongo collection to look at
:type collection: pymongo.collection
:param object_path: mongo object path
:type object_path: str
:param match: mongo search string
:type match: dict
:return: float
"""
return get_field_execute_operation("$avg", collection, object_path, match=match)
......
......@@ -36,6 +36,11 @@ class TestAggregate(MongoDbTest):
result = get_field_average(self.test_collection, "$test_int")
self.assertEqual(result, 4.5)
def test_get_field_sum_match(self):
self.add_simple_test_data()
result = get_field_sum(self.test_collection, "$test_int", match={"test_int": {"$lt": 5}})
self.assertEqual(result, 10)
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment