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

How do I sort in Collection.find_one_and_update? #242

Open
tyteen4a03 opened this issue Nov 6, 2018 · 3 comments
Open

How do I sort in Collection.find_one_and_update? #242

tyteen4a03 opened this issue Nov 6, 2018 · 3 comments

Comments

@tyteen4a03
Copy link

I have the following code:

        next_job = yield self.scrapers_coll.find_one_and_update(
            {"state": "enqueued"},
            {
                "$set": {
                    "startTime": time.time(),
                }
            },
            sort=[("createdOn", pymongo.ASCENDING)]
    )

This gives me a TypeError: list indices must be integers or slices, not str.

From going through the source code it looks like TxMongo wants a different format than PyMongo. How do I specify the sort order in this case?

@IlyaSkriblovsky
Copy link
Contributor

Yes, txmongo has its own syntax for sort, hint and other query options:

from txmongo import filter

next_job = yield self.scrapers_coll.find_one_and_update(
    {"state": "enqueued"},
    {
        "$set": {
            "startTime": time.time(),
        }
    },
    sort=filter.sort(filter.ASCENDING("createdOn"))
)

This way you can sort by multiple fields:

filter.sort(filter.ASCENDING("priority") + filter.ASCENDING("createdOn"))

@tyteen4a03
Copy link
Author

Any plans to update the code to support standard pymongo syntax too?

@IlyaSkriblovsky
Copy link
Contributor

actually, sort= can also be specified like this:

    sort=filter.sort(["createdOn", pymongo.ASCENDING])

Which is almost like in pymongo except wrapping with filter.sort

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

No branches or pull requests

2 participants