Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_mongo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-gitdep
common_helper_mongo
Commits
cab81724
Commit
cab81724
authored
Dec 13, 2016
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
docstrings added; match test added
parent
cab4cfa7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
0 deletions
+43
-0
aggregate.py
common_helper_mongo/aggregate.py
+38
-0
test_aggregate.py
tests/test_aggregate.py
+5
-0
No files found.
common_helper_mongo/aggregate.py
View file @
cab81724
...
...
@@ -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
)
...
...
tests/test_aggregate.py
View file @
cab81724
...
...
@@ -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
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment