Parcourir la source

added indexing method to MongodbHandler

tanja il y a 3 ans
Parent
commit
2eacfa6135
1 fichiers modifiés avec 12 ajouts et 0 suppressions
  1. 12 0
      cdplib/db_handlers/MongodbHandler.py

+ 12 - 0
cdplib/db_handlers/MongodbHandler.py

@@ -308,6 +308,18 @@ class MongodbHandler:
                 collection_name=input_collection_name,
                 aggregation_pipeline=aggregation_pipeline)
 
+    def index_collection(self, collection_name: str, keys: list):
+        """
+        :param keys: compound indexes for the collection,
+         is either a list of tuples of shape (field_name, 1) or (field_name, -1)
+         for the indexing order, or a tuple of field namse, then the second element of the
+         tuple is set to 1
+        """
+        keys = [(key, 1) if not isinstance(key, tuple) else key for key in keys]
+
+        self._database[collection_name].create_index(keys)
+
+
     def aggregate_data_and_generate_dataframe(self, collection_name: str, aggregation_pipeline: list, index: str = None, return_as_dataframe=True):
 
         try: