Giter Club home page Giter Club logo

Comments (7)

roman-right avatar roman-right commented on June 11, 2024

Hi @KarlCMB ,
This is the MongoDB limitation. The code you wrote is translated into updateOne query, which doesn't support sorting: https://www.mongodb.com/docs/manual/reference/method/db.collection.updateOne/ .

But I'll think if it can be supported with a few workarounds. For now I'd suggest fetching and updeting after.

from beanie.

KarlCMB avatar KarlCMB commented on June 11, 2024

Hi @roman-right
thank you for the answer.
If anyone faces the same challenge:
I copied your code from here to solve this issue.

from beanie.

slingshotvfx avatar slingshotvfx commented on June 11, 2024

Would it be possible to implement findAndModify?

I have a queue-style use case much like this but I'd love to be able to find, update, and return the most recent document all in one atomic query.

edit: I see find_and_modify is deprecated in pyMongo, but find_one_and_update might work

from beanie.

KarlCMB avatar KarlCMB commented on June 11, 2024

@slingshotvfx just do a regular find and update with limit=1

from beanie.

slingshotvfx avatar slingshotvfx commented on June 11, 2024

@slingshotvfx just do a regular find and update with limit=1

That works, but returns a PyMongo UpdateResult so you still have to do a second query right after to read the just-updated item from the database.

from beanie.

KarlCMB avatar KarlCMB commented on June 11, 2024

You can use the UpdateResponse to control the behaviour

from beanie.

slingshotvfx avatar slingshotvfx commented on June 11, 2024

UpdateMany doesn't take a response_type argument unfortunately.

So you can find_one and update, return the updated item, but that ignores sort order, or you can find and update with limit=1 which works with sorting but doesn't return the updated item or even the updated item id. Unless I'm missing something?

from beanie.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.