Commit 39b8d5a3 by Peter Weidenbach

get all values and collect additional information function added

parent 1be5dcfa
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'
]
......@@ -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:
......
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)
......
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