@@ -101,7 +101,21 @@ def update_references_mongodb(self, references_file, id_column='key'):
101101 result = self .references .replace_one (filter = {id_column : id_value }, replacement = doc , upsert = True )
102102
103103 def _recursive_json_fix (self , doc ):
104- # Recursively fix a JSON document to convert lists to numpy arrays
104+ """
105+ Recursively fix a JSON document to convert lists to numpy arrays.
106+ This is needed for queries against the MongoDB database.
107+
108+ Parameters
109+ ----------
110+ doc : dict
111+ Document result from a query against a MongoDB database
112+
113+ Returns
114+ -------
115+ out_doc : dict
116+ Fixed document
117+ """
118+
105119 out_doc = {}
106120 for key , val in doc .items ():
107121 if isinstance (val , dict ):
@@ -117,7 +131,21 @@ def _recursive_json_fix(self, doc):
117131 return out_doc
118132
119133 def _recursive_json_reverse_fix (self , doc ):
120- # Undo the work from _recursive_json_fix; that is turn arrays to list to make correct JSON documents
134+ """
135+ Undo the work from _recursive_json_fix; that is turn arrays to list to make correct JSON documents.
136+ This is needed since MongoDB does not understand numpy arrays.
137+
138+ Parameters
139+ ----------
140+ doc : dict
141+ Document result to convert
142+
143+ Returns
144+ -------
145+ out_doct : dict
146+ Fixed document
147+ """
148+
121149 out_doc = {}
122150
123151 # Remove _id if present (used in MongoDB)
@@ -138,6 +166,23 @@ def _recursive_json_reverse_fix(self, doc):
138166 return out_doc
139167
140168 def save_from_db (self , doc , verbose = False , out_dir = '' , save = False , name = '' ):
169+ """
170+ Save a JSON representation of the document. Useful for exporting database contents.
171+
172+ Parameters
173+ ----------
174+ doc :
175+ Document result from a query
176+ verbose : bool
177+ Flag to indicate whether the JSON representation should be printed in the terminal (Default: False)
178+ out_dir : str
179+ Directory to save JSON file (Default: '')
180+ save : bool
181+ Flag to indicate if the JSON represenation should be saved
182+ name : str
183+ Name of output JSON file. If none is provided, the 'name' field is used to name it. (Default: '')
184+ """
185+
141186 # Save a JSON representation
142187 out_doc = self ._recursive_json_reverse_fix (doc )
143188 out_json = json .dumps (out_doc , indent = 4 , sort_keys = False )
@@ -154,6 +199,35 @@ def save_from_db(self, doc, verbose=False, out_dir='', save=False, name=''):
154199 return
155200
156201 def query (self , query , embed_ref = False , ref_id_column = 'key' ):
202+ """
203+ Perform a database query with MongoDB's query language.
204+ Examples:
205+ db.query({'name': 'And XXX'})
206+ db.query({'ra.value': 10.68458})
207+ Query examples:
208+ Greather than case: query = {'surface_brightness.value': {'$gt': 27}}
209+ EXISTS case: query = {'stellar_radial_velocity_dispersion.value': {'$exists': True}}
210+ AND case: query = {'surface_brightness.value': {'$gt': 27}, 'radial_velocity.value': {'$lte': -100}}
211+ OR case: query = {'$or': [{'ra.value': 10.68458}, {'dec.value': 49.64667}]}
212+ AND and OR case: query = {'surface_brightness.value': {'$gt': 27}, '$or': [
213+ {'surface_brightness.error_upper': {'$lte': 0.5}},
214+ {'surface_brightness.error_lower': {'$gte': 1.0}}]}
215+
216+ Parameters
217+ ----------
218+ query : dict
219+ Query to perform. Uses MongoDB's query language.
220+ embed_ref : bool
221+ Flag whether or not references should be embedded in the output document (Default: False)
222+ ref_id_column : str
223+ Field name to use when matching references (Default: 'key')
224+
225+ Returns
226+ -------
227+ result : np.array
228+ Numpy array of document results
229+ """
230+
157231 if self .use_mongodb :
158232 result = self ._query_mongodb (query )
159233 else :
@@ -175,6 +249,22 @@ def query(self, query, embed_ref=False, ref_id_column='key'):
175249 return result
176250
177251 def query_reference (self , query ):
252+ """
253+ Query references. Examples:
254+ db.query_reference({'id': 1})
255+ db.query_reference({'key': 'Bellazzini_2006_1'})[0]
256+
257+ Parameters
258+ ----------
259+ query : dict
260+ Query to use. Uses MongoDB's query language.
261+
262+ Returns
263+ -------
264+ result : np.array
265+ Numpy array of document results
266+ """
267+
178268 if self .use_mongodb :
179269 result = list (self .references .find (query ))
180270 for r in result :
@@ -289,8 +379,24 @@ def _store_quantity(self, val, unit):
289379 return val
290380
291381 def table (self , query = {}, selection = {}):
292- # Get nicely-formatted table of all results (or of the query)
293- # selection is a dictionary with the field value and the reference to use for it (otherwise will pick best=1)
382+ """
383+ Get a formatted table of all query results. When multiple results are present for a single value, the best one
384+ is picked unless the user specifies a selection. This functionality will be revisited in the future.
385+ The output is as a QTable which allows Quantities to be embedded in the results.
386+
387+ Parameters
388+ ----------
389+ query : dict
390+ Query to use in MongoDB query language. Default is an empty dictionary for all results.
391+ selection : dict
392+ Dictionary with the field value and reference to use for it (otherwise will pick best=1)
393+
394+ Returns
395+ -------
396+ df : astropy.table.QTable
397+ Astropy QTable of results
398+ """
399+
294400 results = self .query (query = query )
295401
296402 # For each entry in result, select best field.value or what the user has specified
0 commit comments