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
cab4cfa7
Commit
cab4cfa7
authored
Dec 13, 2016
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
aggregation field operations added
parent
b48e773b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
4 deletions
+37
-4
__init__.py
common_helper_mongo/__init__.py
+5
-2
aggregate.py
common_helper_mongo/aggregate.py
+20
-0
test_aggregate.py
tests/test_aggregate.py
+12
-2
No files found.
common_helper_mongo/__init__.py
View file @
cab4cfa7
from
.gridfs
import
overwrite_file
from
.aggregate
import
get_objects_and_count_of_occurrence
from
.aggregate
import
get_objects_and_count_of_occurrence
,
get_field_average
,
get_field_sum
,
get_field_execute_operation
__all__
=
[
'overwrite_file'
,
'get_objects_and_count_of_occurrence'
'get_objects_and_count_of_occurrence'
,
'get_field_average'
,
'get_field_sum'
,
'get_field_execute_operation'
]
common_helper_mongo/aggregate.py
View file @
cab4cfa7
...
...
@@ -17,3 +17,23 @@ def get_objects_and_count_of_occurrence(collection, object_path, unwind=False, m
result
=
list
(
collection
.
aggregate
(
pipeline
))
logging
.
debug
(
result
)
return
result
def
get_field_sum
(
collection
,
object_path
,
match
=
None
):
return
get_field_execute_operation
(
"$sum"
,
collection
,
object_path
,
match
=
match
)
def
get_field_average
(
collection
,
object_path
,
match
=
None
):
return
get_field_execute_operation
(
"$avg"
,
collection
,
object_path
,
match
=
match
)
def
get_field_execute_operation
(
operation
,
collection
,
object_path
,
match
=
None
):
pipeline
=
[]
if
match
is
not
None
:
pipeline
.
append
({
"$match"
:
match
})
pipeline
.
append
({
"$group"
:
{
"_id"
:
"null"
,
"total"
:
{
operation
:
object_path
}}})
tmp
=
collection
.
aggregate
(
pipeline
)
result
=
0
for
item
in
tmp
:
result
=
item
[
'total'
]
return
result
tests/test_aggregate.py
View file @
cab4cfa7
from
common_helper_mongo.aggregate
import
get_objects_and_count_of_occurrence
from
common_helper_mongo.aggregate
import
get_objects_and_count_of_occurrence
,
\
get_field_sum
,
get_field_average
import
unittest
from
tests.base_class_database_test
import
MongoDbTest
...
...
@@ -24,7 +25,16 @@ class TestAggregate(MongoDbTest):
result
=
get_objects_and_count_of_occurrence
(
self
.
test_collection
,
"$test_txt"
,
unwind
=
"False"
,
match
=
{
"test_int"
:
0
})
self
.
assertEqual
(
len
(
result
),
1
,
"number of results not correct"
)
self
.
assertEqual
(
result
[
0
][
'_id'
],
"item 0"
)
print
(
result
)
def
test_get_field_sum
(
self
):
self
.
add_simple_test_data
()
result
=
get_field_sum
(
self
.
test_collection
,
"$test_int"
)
self
.
assertEqual
(
result
,
45
)
def
test_get_field_avg
(
self
):
self
.
add_simple_test_data
()
result
=
get_field_average
(
self
.
test_collection
,
"$test_int"
)
self
.
assertEqual
(
result
,
4.5
)
if
__name__
==
"__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