Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I thought it would be useful for me to give you a bit of background information before fully answering your question:</p> <p>When mongo creates an index on a collection it is essentially sorting the collection based on the index key. Thus, when you get the results from a query that uses an index, the results are going to be sorted by key for that index. If your query uses a basic index on field1, then the results are, by definition, sorted on that index.</p> <p>There are a few ways to get around this however. One simple way would be to</p> <p>1) Query on field1</p> <p>2) Sort the result of 1) using field3</p> <p>This would, if I understand your comment correctly, get you your desired output. However, it has the downside of requiring 2 major pieces of work, which could be costly depending on the size of your data.</p> <p>There is another way to solve your problem: compound indexes. Compound indexes use multiple keys to generate an index that is sorted based on both of those keys. There is some documentation here: <a href="http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys" rel="nofollow">http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys</a> and there are quite a few stack-overflow questions about compound indexes if you want to dive more deeply into the subject.</p> <p>Also, if you want some general info on indexing (and compound indexing) in mongodb check out <a href="http://www.mongodb.org/display/DOCS/Indexing+Advice+and+FAQ#IndexingAdviceandFAQ-1.Thesortcolumnmustbethelastcolumnusedintheindex" rel="nofollow">http://www.mongodb.org/display/DOCS/Indexing+Advice+and+FAQ#IndexingAdviceandFAQ-1.Thesortcolumnmustbethelastcolumnusedintheindex</a>. this whole document should be really useful to you I expect.</p> <p>Using compound indexes, while elegant and appropriate, is definitely more complex than just using sort after a query. If you are willing to spend the time looking into it though I am sure you would find it will help you find a nice solution.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload