Converting aggregation results to umongo objects
See original GitHub issueI’m using umongo to gather documents using mongos aggregation framework:
MyDocumentClass.collection.aggregate([
{'$match': {
'field1': value1,
'field2': value2
}},
# more pipes
])
The resulting documents are plain dictionaries. It wouldn’t be that bad since i want to convert them to json for serving over an api anyway, but i loose umongos nice integration for dump()
that way and i would have to convert objectIDs, Dates etc myself.
My current solution is iterating over the results and converting them into my desired umongo Class
raw_docs = await above_future.to_list(None)
docs = [MyDocumentClass.build_from_mongo(docs) for docs in raw_docs]
Is there a better way to do this or what is your recommended solution for converting aggregate results to umongo Document objects (i can totally see why you wouldn’t do this automatically since aggregation results don’t have to be anyway structured like modeled Document classes)?
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
How to convert the result object from aggregation pipeline ...
The aggregation call returns AggregateIterable. How do I map this to a List? Am I using the completely wrong approach here?
Read more >User guide — uMongo 3.1.0 documentation - Read the Docs
Document allows you to work with your data as objects and to guarantee their validity against a model. First let's define a document...
Read more >Convert array of objects, to object - mongodb
I have tried few ways but i am unable to produce output. We can't use unwind otherwise it will break the relationship between...
Read more >UMongo 1.6.0 released - big release with...
UMongo 1.6.0 released - big release with Aggregation framework, ... With a checklist you go through all items every time it is used, ......
Read more >howto map the log4mongo Document to java object- ...
[Solved]-howto map the log4mongo Document to java object-mongodb. Search. score:1. Accepted answer. morphia.map is instance method. You need to morphia.map ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t get the trouble: given the result of your aggregation is the document itself why your first solution doesn’t work anymore ?
Beside, I think we can make thing more elegant for your needs by adding a
bind_as
attribute to theaggregate
function in order to do the construction of theDocuments
inside the returned cursor:What do you think ?
Closing this as