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

Migrations: Implement adapter method copy_value_from_field_to_field_in_each_document #2259

Closed
eecavanna opened this issue Nov 12, 2024 · 4 comments · Fixed by #2262
Closed
Assignees
Labels
enhancement New feature or request X SMALL Less than 8 hours, less than 1 day

Comments

@eecavanna
Copy link
Collaborator

Adapter method signature:

def copy_value_from_field_to_field_in_each_document(
    self,
    collection_name: str,
    source_field_name: str,
    destination_field_name: str,
) -> None:
    r"""
    For each document in the collection that has the source field,
    copy the value of the source field into the destination field,
    creating the destination field if it doesn't already exist.
    If the document lacks the source field, do nothing.
    """
    pass

Mongo query:

db.getCollection("things").updateMany({
    {"foo": {$exists: true}},   // for each document that has a `foo` field,
    [{"$set": {"bar": "$foo"}}] // populate its `bar` field with the value in that `foo` field
});
@eecavanna eecavanna added enhancement New feature or request X SMALL Less than 8 hours, less than 1 day labels Nov 12, 2024
@eecavanna eecavanna self-assigned this Nov 12, 2024
@eecavanna
Copy link
Collaborator Author

Hi @kheal and @brynnz22, I'm planning to implement the adapter method described above. I wanted to know whether you find its signature (i.e. name and parameters) to be intuitive. I plan to implement it as described above unless I hear of any concerns.

Note: Separately, I'm planning to implement an adapter method that can be used to delete a field from each document in the collection. Using the above adapter method and this delete one, together, a migrator author will be able to effectively rename a field (which is being done in a different way here).

@brynnz22
Copy link
Contributor

Yeah this makes sense to me! And would be very helpful

@kheal
Copy link
Contributor

kheal commented Nov 12, 2024

This looks great.

@eecavanna
Copy link
Collaborator Author

eecavanna commented Nov 12, 2024

Thanks! I have opened a PR and designated both of you as reviewers.

I'm going to jump straight into implementing a second adapter methoddelete_field_from_each_document—now (in a different PR). I'll tag you as reviewers there, too (ETA is 1 hour). In case you prefer waiting until the second PR exists before doing any reviewing, that would work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request X SMALL Less than 8 hours, less than 1 day
Projects
Status: Done
3 participants