Reading from primary and secondary changing the order of the documents of the same query

Hi,

I am executing a query from Primary and then Secondary, the order (sequence of the result is different).
When I am executing a query using _id sort, then the order is same.
I there anyway we can have same query results without doing sorting on _id field.

What is your query like?

Hi Kobe,

these is my sample query:

mongos> db.products.find().readPref(“primary”)
{ “_id” : 10, “item” : “1”, “qty” : 20 }
{ “_id” : 11, “item” : “2”, “qty” : 20 }
{ “_id” : 12, “item” : “3”, “qty” : 20 }
mongos> db.products.find().readPref(“secondary”)
{ “_id” : 10, “item” : “1”, “qty” : 20 }
{ “_id” : 12, “item” : “3”, “qty” : 20 }
{ “_id” : 11, “item” : “2”, “qty” : 20 }

Here orders getting changed

If no sort condition is specified, natural ordering is used by mongodb.

This is internal implementation so relying on this order is almost always bad practice. You should explicitly use a sort (e.g. on _id). Otherwise the order of returned docs is nondeterministic.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.