umongo icon indicating copy to clipboard operation
umongo copied to clipboard

Feature request: fetch all documents in Reference FIeld list with projection

Open halfdanrump opened this issue 5 years ago • 3 comments

Hi there!

Using the example class from the docs:

@instance.register
class User(Document):
    email = fields.EmailField(required=True, unique=True)
    birthday = fields.DateTimeField(validate=validate.Range(min=datetime(1900, 1, 1)))
    friends = fields.ListField(fields.ReferenceField("User"))

    class Meta:
        collection = db.user

I'd like to be able to do a query like

friends = User.find_one().friends
friends.fetch_all(projection=['name'])

Does it make sense?

Thank you :)

halfdanrump avatar May 22 '19 04:05 halfdanrump

For the record, I'll be happy to work on this if you think the feature makes sense :)

halfdanrump avatar May 22 '19 05:05 halfdanrump

IIUC, this is two features.

  • Add projection to fetch. Already discussed in https://github.com/Scille/umongo/issues/98. It revealed more complicated than I first thought and I gave it up. I still think it would be a nice feature. For this feature, let's continue discussion there.

  • Add fetch_all method to List. Awkward because a ListField can contain any field, not necessarily EmbeddedField.

    With projection in fetch, you could do

    fetched_friends = [f.fetch(projection=['name']) for f in friends]
    

    Not as straightforward but not that bad.

lafrech avatar May 22 '19 07:05 lafrech

You're right, these are two separate features

With projection in fetch, you could do

fetched_friends = [f.fetch(projection=['name']) for f in friends]

Won't this send n queries to mongo where n is the number of friends?

Leaving aside the projection for now, how about a solution where

User.find_one().friends.fetch_all()

works if and only if the entries in the ListField is a ReferenceField?

halfdanrump avatar May 30 '19 06:05 halfdanrump