question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting aggregation results to umongo objects

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
touilleMancommented, Aug 13, 2016

My Problem now is that i have to define a Document subclass if i want to use umongos abstraction and another Schema subclass, thats nearly the same, for using aggregation. Resulting problems when doing this are inconsistences f.e. my ID field is called id on umongo and _id on marshmallow schemas.

I don’t get the trouble: given the result of your aggregation is the document itself why your first solution doesn’t work anymore ?

raw_docs = await above_future.to_list(None)
docs = [MyDocumentClass.build_from_mongo(docs) for docs in raw_docs]

Beside, I think we can make thing more elegant for your needs by adding a bind_as attribute to the aggregate function in order to do the construction of the Documents inside the returned cursor:

@instance.register
class AggregatedObj(EmbeddedDocument):
    field1 = fields.StrField()
    field2 = ...

results = MyDocumentClass.collection.aggregate([
            {'$match': {
                'field1': value1,
                'field2': value2
            }},
            # more pipes
        ], bind_as=AggregatedObj)

(await results.to_list(None))[0]
# <object EmbeddedDocument __main__.AggregatedObj({'field1': ...})>

What do you think ?

0reactions
lafrechcommented, Jan 4, 2021

Closing this as

the result of the aggregation can be very different to the orginal document

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found