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

  • Home
  • SEARCH
  • 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 8787139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:48:40+00:00 2026-06-13T21:48:40+00:00

I wanted to implement a function which needs the values from a pymongo collection

  • 0

I wanted to implement a function which needs the values from a pymongo collection in the reverse order of inserting.

I can think of a couple of ways:

cursor = collection.find(skip=collection.count()-LIMIT_VAL,limit=LIMIT_VAL)
rows = list(cursor).reverse()

This way would be a little bad since LIMIT_VAL is a variable and I do not want to put the entire thing into memory. I do not want to supply -ve value to the skip parameter.

OR

rows = collection.find(sort={'$natural':-1}, limit=LIMIT_VAL)

I read here that you can not really rely on natural sort order. I do not know how that can be unreliable

OR

I insert a value called order_of_insert into each record and do a sort on that ensuring that I get how I have inserted.

Before I implement this I needed to make sure which of the ways would be the best thing to do.

  • 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-13T21:48:42+00:00Added an answer on June 13, 2026 at 9:48 pm

    Mongodb ObjectIDs store time-stamp information accurate up to the second. $natural sorts it by this _id. If your app were to insert a document within the same second, the insertion order would be ambiguous. $natural would correspond to insertion order only if your collection were capped.

    1. If your application can tolerate mostly accurate insertion order and you need data to persist forever, use $natural without capping the collection.
    2. If you need accurate insertion order and the data set is small/semi-persistent, cap the collection and use $natural sorting
    3. Otherwise it looks like you’ll need to sort on your own time-stamp or some other field to keep track of order.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to implement a default sort order in my domain class and immediately
If I wanted to implement an app for 2 Android devices so I can
I wanted to implement my own debug function that has the same signature as
Since this function is implemented in IE8, I wanted to see exactly what I
I wanted to implement a Node type like written below but I want to
I wanted to implement a search feature for my web app. I choose Haystack
I wanted to implement python com server using win32com extensions. Then consume the server
I wanted to implement the game Pacman. For the AI, I was thinking of
I wanted to implement a simple python program using parallel execution. It's I/O bound,
I always wanted to implement swypable tabs in my application, like the ones in

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.