I have a case that looks like this:
I have a collection called for example “Test” and I have set up an index for it and the index looks something like this:
{
"A" : 1,
"B" : 1,
"C" : 1
}
In my code I am retrieving the records based on “A” being a certain value:
List<Test> tests = Test.Find(Query.EQ("A", CertainValue)).ToList();
And to my surprise all the records are coming back sorted in the order mentioned in the index (“B” ascending then “C” ascending)
I was wondering if this behavior is always to be expected, is the records always going to be sorted based on the index, or am I just imagining stuff and this is just a one off case?
PS. The Records are NOT entered into the collection sorted so this is not the natural order of the records.
Just as with many other dictionary implementations, order in database is not defined or guaranteed (except for capped collections) and subject to change at any time:
Use explicit sort if you need them sorted in some way or refer to your particular driver (C#) implementation docs to see if it sorts them for you.