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 9214495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:59:03+00:00 2026-06-18T01:59:03+00:00

How do i iterate over all the documents in a collection sorted by my

  • 0

How do i iterate over all the documents in a collection sorted by my unique id (uid) field to retrieve the `”eng” field?

My document structure looks like this:

{ "_id" : ObjectId("50fd55c1161747668078fed6"), "dan" : "Hr. Hänsch repræsenterede Dem dér .", "deu" : "Der Kollege Hänsch hat Sie dort vertreten .", "eng" : "Mr Hänsch represented you on this occasion .", "fin" : "Kollega Hänsch edusti teitä siellä .", "ita" : "Il collega Hänsch è intervenuto in sua vece .", "md5" : "336b9cd1dc01ae0ff3344072d8db0295", "uid" : 2100986 }

Why doesn’t the sort work:

connection = MongoClient()
db = connection["europarl"] # The database.
v7 = db["v7"] # The collection

for i in v7.find({"eng":{"$exist":True}}).sort({"uid":-1}):
    print i["eng"]

I get this TypeError:

Traceback (most recent call last):
  File "mongo_lemma.py", line 67, in <module>
    for i in v7.find({"eng":{"$exists":True}}).sort({"uid":"1"}):
  File "/usr/local/lib/python2.7/dist-packages/pymongo-2.4.1-py2.7-linux-x86_64.egg/pymongo/cursor.py", line 513, in sort
    keys = helpers._index_list(key_or_list, direction)
  File "/usr/local/lib/python2.7/dist-packages/pymongo-2.4.1-py2.7-linux-x86_64.egg/pymongo/helpers.py", line 47, in _index_list
    raise TypeError("if no direction is specified, "
TypeError: if no direction is specified, key_or_list must be an instance of list

When i did this, it worked:

for i in v7.find({"eng":{"$exist":True}}):
    print i["eng"]
  • 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-18T01:59:04+00:00Added an answer on June 18, 2026 at 1:59 am

    Your sort() syntax is off, it should be:

    for i in v7.find({"eng":{"$exist":True}}).sort("uid", pymongo.DESCENDING):
        print i["eng"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to iterate over all childs of a jQuery's .children() return value, like
I'd like iterate over every document in a (probably big) Lotus Domino database and
I'd like to iterate over a list of distinct fields in a given document.
How can I iterate over all documents matching each value of a specified key
I would like to iterate over an XML document that is essentially a list
I'm trying to iterate over all main menu's items. Problem starts at sub menus
Does Python offer a way to iterate over all consecutive sublists of a given
I have a 3D array in Python and I need to iterate over all
I have a boost::bimap and I want to iterate over all positions to add
I have a Hashtable in Java and want to iterate over all the values

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.