0

We are using Mongo 2.6 in our application. We upgraded from 2.4 to 2.6 version.

Will the order be maintained in case of mongo 2.6 query based on different individual indexes.

Thanks.

Vipin
  • 781
  • 1
  • 10
  • 19
  • 1
    You cannot use index inter-sectioning for sorting yet so using it multiple queries for one index ordering is an invalid question currently. – Sammaye Jun 16 '14 at 13:13
  • Ordering means the insertion order into database not the sorting one. – Vipin Jun 16 '14 at 13:57
  • Can you clarify your question? It's not clear what you're asking. – JohnnyHK Jun 16 '14 at 13:59
  • The insertion order into the database will always be the same, natural order, find order will be by natural or rather logical ordering. You must imply an ordering for the return to be ordered, as for ordering storage...I am unsure if there is a way currently. – Sammaye Jun 16 '14 at 14:03
  • will Query return the data with the same order that was inserted into the database – Vipin Jun 16 '14 at 14:04
  • No it will in fact return logical order, which is order of the internal trees, basically it the tree will modify on every operation, including update, not just insertion. – Sammaye Jun 16 '14 at 14:19
  • 1
    If you are using the default _id value built by mongo, use sort("_id": 1) to force return based on order it was inserted. – kwolfe Jun 16 '14 at 14:38
  • If you want a deterministic order you need to specify `sort` criteria. See also: [What does Mongo sort on when no sort order is specified?](http://stackoverflow.com/questions/11599069/). – Stennie Jul 05 '14 at 13:11

1 Answers1

1

As long as you explicitly use sort() in your queries, the order will remain the same in version 2.4. If your queries don't specify sorting order using sort(), there is a change in Mongo DB 2.6 that may affect the result order of your queries. MongoDB can now use the intersection of multiple indexes to fulfill queries. If you have two individual indexes:

db.collection.ensureIndex({A: 1})
db.collection.ensureIndex({B: 1})

and you don't have the compound index:

db.collection.ensureIndex({A: 1, B: 1})

The query db.collection.find({A : a, B: b}) will return the results sorted by both A and B in version 2.6. In version 2.4, it is sorted by A only. For more details, check out this http://docs.mongodb.org/manual/core/index-intersection/

anhlc
  • 13,839
  • 4
  • 33
  • 40