umongo
umongo copied to clipboard
Feature request: fetch all documents in Reference FIeld list with projection
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 :)
For the record, I'll be happy to work on this if you think the feature makes sense :)
IIUC, this is two features.
-
Add
projection
tofetch
. 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 toList
. Awkward because aListField
can contain any field, not necessarilyEmbeddedField
.With
projection
infetch
, you could dofetched_friends = [f.fetch(projection=['name']) for f in friends]
Not as straightforward but not that bad.
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
?