Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:14:46+00:00 2026-06-02T12:14:46+00:00

With PyMongo, when I try to retrieve objects sorted by their ‘number’ and ‘date’

  • 0

With PyMongo, when I try to retrieve objects sorted by their ‘number’ and ‘date’ fields like this:

db.test.find({"number": {"$gt": 1}}).sort({"number": 1, "date": -1})

I get this error:

TypeError: if no direction is specified, key_or_list must be an instance of list

What’s wrong with my sort query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:14:47+00:00Added an answer on June 2, 2026 at 12:14 pm

    sort should be a list of key-direction pairs, that is

    db.test.find({"number": {"$gt": 1}}).sort([("number", 1), ("date", -1)])
    

    The reason why this has to be a list is that the ordering of the arguments matters and dicts are not ordered in Python < 3.6

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How use sql like in PyMongo? >>> db.houses.find().count() 11616 >>> db.houses.find({hid:u16999}).count() 1 >>> db.houses.find({hid:u/9/}).count()
How to find names of all collections using PyMongo and find all fields in
results = docDB.posts.find({active:True }).sort(pop_score, pymongo.DESCENDING) This is my sort right now. But the problem
This is my code: con = pymongo.Connection('test',27017) and the error is: (mysite)zjm1126@zjm1126-G41MT-S2:~/桌面/test/mongo$ python a.py
In Flask-PyMongo, they use self._Collection__database to represent the database object that this Collection object
I have strange behaviour in Python/PyMongo. dbh = self.__connection__['test'] first = dbh['test_1'] second =
I have code in Python/PyMongo like ( code should simulate inner join and all
How to make a selection PyMongo only unique records? >>> db.houses.find({street:{$regex: Fl, $options:i}}).count() 107
Having a weird problem with Python's unittest and PyMongo. The test randomly succeeds or
con = pymongo.Connection(MONGO_DOC_HOST, MONGO_DOC_PORT) db = con.testdb datasets = db.datasets for post in db.datasets.find({test_set:xyz}).sort(num_favs,pymongo.DESCENDING).limit(2):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.