Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project only the fields that are in our model #16

Closed
nzsmith1 opened this issue Apr 16, 2021 · 1 comment · Fixed by #18
Closed

Project only the fields that are in our model #16

nzsmith1 opened this issue Apr 16, 2021 · 1 comment · Fixed by #18
Assignees

Comments

@nzsmith1
Copy link
Contributor

nzsmith1 commented Apr 16, 2021

First of all, thanks for creating this project, it has been fantastic to work with. I have a feature request, which I am happy to implement, I would like the find, find_many, find_all to project our model so that we get back only the data we are interested in.

This helps reduce database load.

An example of where we might use this is when we have a database model which hold a whole bunch of data, but we are loading a summary view which only requires 2 or 3 of these fields.

example:

class AnalyticsLabellingTask(Document):
    reported_by: str = Field(...)
    device_id: str = Field(...)
    priming_start_date: datetime = Field(...)
    event_start_date: datetime = Field(...)
    end_date: datetime = Field(...)
    reviewers: List[str] = Field(...)
    comments: List[Comments] = Field(default_factory=list)
    reviewed_events: List[ReviewedEvents] = Field(default_factory=list)
    water_data: List[TimeSeriesData] = Field(...)
    archived: bool = Field(...)

    class Collection:
        name = "AnalyticsLabellingTasks"

class AnalyticsLabellingTasksSummary(Document):
    device_id: str = Field(...)
    event_start_date: datetime = Field(...)
    end_date: datetime = Field(...)
    reported_by: str = Field(...)

    class Collection:
        name = "AnalyticsLabellingTasks"

I am happy to implement this, I would do it by updating the find calls:

# get the field list of interest, this could be generated
# at the __init__ stage and be stored in memory to prevent
# having to build this many times

fields = test.__fields__
projection = {}
for name, field in fields.items():
    if field.alias:
        projection[field.alias] = 1
    else:
        projection[name] = 1

cursor = cls.get_motor_collection().find(filter_query, projection, **kwargs)

Let me know your thoughts and I can put together a PR next week

@roman-right
Copy link
Member

Hello Nicholas,

Thank you for your feedback.
Yes, this is a great idea. It will so good if you will implement this.

I still didn't add development guides. If you will need any help with this (like how to test and etc.) you can ask here or in Discord.

Thank you.

@nzsmith1 nzsmith1 mentioned this issue Apr 19, 2021
@roman-right roman-right linked a pull request Apr 19, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants