0

Given that MongoDB query results are returned in the order that they are found, which "may coincide with insertion order (but isn't guaranteed to be) or the order of the index(es) used":

Does this mean that the order of the results could change after using Pymongo's rewind() function?

It seems like rewind() performs another database query, right?

Community
  • 1
  • 1
diazdeteran
  • 1,144
  • 1
  • 13
  • 25

1 Answers1

2

Correct, rewind() performs another database query, as if the first had never happened. If you don't specify any sort order to your results, and if MongoDB had to move some documents (because some changed size, for example) between the first and the second query, you will get them in different order.

If you need your documents in a particular order, use sort.

http://api.mongodb.com/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.sort

A. Jesse Jiryu Davis
  • 23,641
  • 4
  • 57
  • 70