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
39b8d5a3
Commit
39b8d5a3
authored
Dec 14, 2016
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get all values and collect additional information function added
parent
1be5dcfa
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
3 deletions
+54
-3
__init__.py
common_helper_mongo/__init__.py
+3
-2
aggregate.py
common_helper_mongo/aggregate.py
+41
-0
test_aggregate.py
tests/test_aggregate.py
+10
-1
No files found.
common_helper_mongo/__init__.py
View file @
39b8d5a3
from
.gridfs
import
overwrite_file
from
.aggregate
import
get_objects_and_count_of_occurrence
,
get_field_average
,
get_field_sum
,
get_field_execute_operation
,
get_list_of_all_values
from
.aggregate
import
get_objects_and_count_of_occurrence
,
get_field_average
,
get_field_sum
,
get_field_execute_operation
,
get_list_of_all_values
,
get_list_of_all_values_and_collect_information_of_additional_field
__all__
=
[
'overwrite_file'
,
...
...
@@ -7,5 +7,6 @@ __all__ = [
'get_field_average'
,
'get_field_sum'
,
'get_field_execute_operation'
,
'get_list_of_all_values'
'get_list_of_all_values'
,
'get_list_of_all_values_and_collect_information_of_additional_field'
]
common_helper_mongo/aggregate.py
View file @
39b8d5a3
...
...
@@ -33,6 +33,47 @@ def get_list_of_all_values(collection, object_path, unwind=False, match=None):
return
result
def
get_list_of_all_values_and_collect_information_of_additional_field
(
collection
,
object_path
,
additional_information_object_path
,
unwind
=
False
,
match
=
None
):
"""
Get a list of unique values and a collection of additional information 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 additional_information_object_path: field of the additional information
:type additional_information_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
})
pipeline
.
extend
([
{
"$group"
:
{
"_id"
:
object_path
,
"additional_information"
:
{
"$addToSet"
:
"$_id"
}}},
{
"$sort"
:
SON
([(
"_id"
,
1
)])}
])
if
unwind
:
old_pipe
=
pipeline
pipeline
=
[{
"$unwind"
:
object_path
}]
pipeline
.
extend
(
old_pipe
)
result
=
list
(
collection
.
aggregate
(
pipeline
))
result
=
_get_dict_from_aggregat_list
(
result
)
logging
.
debug
(
result
)
return
result
def
_get_dict_from_aggregat_list
(
ag_list
):
result
=
{}
for
item
in
ag_list
:
result
[
item
[
'_id'
]]
=
item
[
'additional_information'
]
return
result
def
_get_list_of_aggregate_list
(
ag_list
):
result
=
[]
for
item
in
ag_list
:
...
...
tests/test_aggregate.py
View file @
39b8d5a3
from
common_helper_mongo.aggregate
import
get_objects_and_count_of_occurrence
,
\
get_field_sum
,
get_field_average
,
get_list_of_all_values
get_field_sum
,
get_field_average
,
get_list_of_all_values
,
\
get_list_of_all_values_and_collect_information_of_additional_field
import
unittest
from
tests.base_class_database_test
import
MongoDbTest
...
...
@@ -25,6 +26,14 @@ class TestAggregate(MongoDbTest):
self
.
assertEqual
(
len
(
result
),
4
)
self
.
assertEqual
(
result
[
0
],
"a"
)
def
test_get_list_of_all_values_and_collect_information_of_additional_field
(
self
):
self
.
add_list_test_data
()
result
=
get_list_of_all_values_and_collect_information_of_additional_field
(
self
.
test_collection
,
"$test_list"
,
"$_id"
,
unwind
=
True
,
match
=
None
)
self
.
assertIsInstance
(
result
,
dict
,
"result should be a dict"
)
self
.
assertEqual
(
len
(
result
.
keys
()),
4
,
"number of results not correct"
)
self
.
assertEqual
(
len
(
result
[
'c'
]),
2
,
"c should have two related object ids"
)
self
.
assertEqual
(
len
(
result
[
'a'
]),
1
,
"a should have one related object id"
)
def
test_get_objects_and_count_of_occurence
(
self
):
self
.
add_simple_test_data
()
result
=
get_objects_and_count_of_occurrence
(
self
.
test_collection
,
"$test_txt"
,
unwind
=
False
,
match
=
None
)
...
...
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