Mongodb Skip() and limit()

Mrunmayee picture Mrunmayee · Feb 22, 2016 · Viewed 13.5k times · Source

I am performing update queries on a mongo collection with a lot of records. So I am using skip() and limit() functions. eg: In first file I am working on first 50000 records.

cur1= db.collection_name.find(no_cursor_timeout=True).skip(0).limit(50000)

In send file I am accessing next 50000 records

cur2=db.collection_name.find(no_cursor_timeout=True).skip(50000).limit(50000)

I perform various update queries on these two cursors in separate files. Now I want to know how these updated records are added back in collection? Will cur2 fetch records updated in cur1 queries?

Answer

SiddAjmera picture SiddAjmera · Feb 22, 2016

It might. There's no guarantee as such that you'll get the records In the same order. Whenever a document is inserted or updated, the order in which the docs were stored in a collection also change. And that order is quite random.

Try using Robomongo, to see this nature of MongoDB in action. Whenever you try to get the documents, after every update or insert operation on the college, the order of the records change.

What I would suggest in this case is to sort the records on a field that's unique and monotonically increasing. That way you'll have the surety of getting unedited records in the second cursor.

Hope this helps.