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

Issue with updating a document #116

Closed
mariamgarchagudashvili opened this issue Apr 30, 2020 · 3 comments
Closed

Issue with updating a document #116

mariamgarchagudashvili opened this issue Apr 30, 2020 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@mariamgarchagudashvili
Copy link

Hi, I am having trouble to update a document.
My model:

class MyModel(MongoDBTimeStampedModel):
    id: UUID
    other: str

Usage:

MyModel.update_one(
     {'id': UUID(id)},
     update={
            '$set': { 'other' : 'something'}
        }
)

Is it a bug in passing kwargs to await collection.update_one( filter_kwargs, kwargs, session=session ) should not it be await collection.update_one( filter_kwargs, **kwargs, session=session ) or am I getting something wrong?
Ref: Source code

@levchik
Copy link
Contributor

levchik commented May 18, 2020

Hi, @mariamgarchagudashvili

You're right, kwargs are the params that are passed through to actual pymongo's own update_one:

So the pymongo syntax:

db.collection.update_one({'x': 1}, {'$inc': {'x': 3}})

It takes two dicts: first are the dict with filters, and the second is the actual update statement.

Usage of models update_one in fastapi_contrib is:

await Model.update_one(filter_kwargs={"id": 1}, id=2)

Note that you need to be specific with the filter_kwargs keyword in order to correctly separate filters and update kwargs.

Inside of this Model's statement we do actual call to collection:

res = await collection.update_one(
    filter_kwargs, kwargs, session=session
)

Here, filter_kwargs and kwargs (as accepted with **kwargs) are dicts, so I assume the code inside is correct.

Back to your code, I think that changing it to something like the following would solve the issue:

MyModel.update_one(
     filter_kwargs={'id': UUID(id)},
     other='something'
)

or with more complex statement:

MyModel.update_one(
     filter_kwargs={'id': UUID(id)},
    **{'$set': { 'other' : 'something'}}
)

@levchik levchik self-assigned this May 18, 2020
@levchik levchik added the question Further information is requested label May 18, 2020
@mariamgarchagudashvili
Copy link
Author

mariamgarchagudashvili commented May 18, 2020

Hi @levchik ,
Thanks a lot for clarifying. It worked :)
It confused me as I could not pass '$set' as kwargs

@levchik
Copy link
Contributor

levchik commented Jun 18, 2020

No worries, @mariamgarchagudashvili

I'm closing this issue now, if you have new questions, please open a new issue. Thanks.

@levchik levchik closed this as completed Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants