Ming: How to extract dictionary from document in MongoDB? A solution is proposed.

31 views
Skip to first unread message

David Santana Gómez

unread,
Jul 2, 2021, 3:54:03 AM7/2/21
to TurboGears

Hello everybody.
We want to do a search using ming and from the results obtain the documents in dictionary format in order to manipulate it or save it in csv.
We honestly have not found about this in the documentation and ming only returns an object in the search.
Therefore, we have looked at the code and we propose the following to obtain the dictionary but we do not know if it is the most optimal or correct.
We have the following ming object
In [8]: full = BaseGobcanApp.query.find().all()                                                                                       
                                                                                                                                      
In [9]: full[0]                                                                                                                       
Out[9]:                                                                                                                               
<BaseGobcanApp _id=ObjectId('60dc561b8eb792f353c19e24')                                                                               
  _version=1 app_id=524 full_name='PRIVADO' area_id=12                                                                                          
  servicio='Aplicaciones Corporativas' servicio_id=4                                                                                  
  updated=datetime.datetime(2021, 6, 30, 13, 11, 52, 961000)                                                                          
  from_full_scrap=True>   
And with this command we get its dictionary
In [24]: full[0].__ming__.state.document
Out[24]:
{'_id': ObjectId('60dc561b8eb792f353c19e24'),
 '_version': 1,
 'app_id': 524,
 'area': 'ADMINISTRACIONESPUBLICAS',
 'area_id': 12,
 'from_full_scrap': True,
 'full_name': 'PRIVADO',
 'servicio': 'Aplicaciones Corporativas',
 'servicio_id': 4,
 'updated': datetime.datetime(2021, 6, 30, 13, 11, 52, 961000)}
Questions:
  • This is correct? or is there another way to do it?

Alessandro Molina

unread,
Jul 2, 2021, 4:03:39 AM7/2/21
to TurboGears
It's mostly correct, but it would be better if you went through the public api

state(obj).document


Note by the way, that will grab the document as is in memory, so it won't go through any default or property access side effect that your MappedClass might expose. In theory, if your goal is to convert a mapped object to a dictionary it's better to threat it like any other python object and dump it. If you are using TurboGears, there is for example the tg.util.ming.dictify function that does exactly that. Or you can take inspiration ( https://github.com/TurboGears/tg2/blob/development/tg/util/ming.py#L25-L35 )

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/turbogears/89c8f382-4ed5-4587-9222-03bb61fd4cbcn%40googlegroups.com.

David Santana Gómez

unread,
Jul 6, 2021, 7:56:07 AM7/6/21
to TurboGears
Thanks for answering me so quickly and the advice. :)
Reply all
Reply to author
Forward
0 new messages