Let's say I have a mongodb database with collection. Then, my collection has a field "id" (not the unique id of nature order). This is my own field, and this "id" field is sorted ascending by default in my collection. What I want to do is, making the collection sort it differently by default, like descending or somehow, not ascending like before. Is there any way to do that? I use C# as my main language, but I can use mongo shell, or mongo compass as well to do this if neccessary.
Asked
Active
Viewed 46 times
0
-
The default sort order is not defined. If you want results sorted by ascending or descending order you need to explicitly specify a sort order in your query. An index can efficiently support queries matching the query pattern or the inverse of the query pattern, so for example an index on a single field like `id` supports sorting in either ascending or descending order. For more information see: [Use Indexes to Sort Query Results](https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/#sort-on-multiple-fields) in the MongoDB documentation. – Stennie Jan 18 '19 at 22:04
-
I did that, but I think the sort does not change the location of the documents in the collection. Is there any way to do that? – maxrena Jan 19 '19 at 19:43
-
The location and ordering of documents on disk is determined by the storage engine, not the end user. You currently need to specify an explicit sort order in order to get a predictable sort outcome based on field value(s). I think the general feature you are looking for is a [clustered index](https://en.wikipedia.org/wiki/Database_index#Clustered), where the physical location matches the order of the clustered index. You may want to watch/upvote [SERVER-3294: Ability to keep disk in index order](https://jira.mongodb.org/browse/SERVER-3294) in the MongoDB Jira issue tracker. – Stennie Jan 19 '19 at 23:35