mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

add parameter `field_name` to in_bulk queryset method

Open aryaniyaps opened this issue 4 years ago • 1 comments

the django ORM has a similar function to the existing in_bulk method.

https://docs.djangoproject.com/en/3.2/ref/models/querysets/#in-bulk

https://github.com/MongoEngine/mongoengine/blob/96802599045432274481b4ed9fcc4fad4ce5f89b/mongoengine/queryset/base.py#L722-L745

It also provides an additional field_name parameter, which makes it easy to load objects in bulk not only by their IDs, but also, by any field. For example, someone might want to bulk load users by their usernames.

It would be convenient if mongoengine also provided the same feature. What do you think of this idea? I'd love to make a PR if you're okay with this.

aryaniyaps avatar Aug 11 '21 05:08 aryaniyaps

Hi @codebyaryan, I actually didn't know about in_bulk so thanks for pointing that out :)

I believe it's a good idea, few remarks if you start getting your hands dirty on this:

  • Mongo being more flexible than sql, this won't work for all the fields but I suggest to not try to fix fields that can't be used as key in the dict, if user use it with funky fields, it will fail immediatly and that is acceptable to me
  • make sure to add some note in the docstring to warn that using it on non-unique fields will lead to inconsistencies

bagerard avatar Aug 23 '21 20:08 bagerard