0

I frequently find myself (for better or worse) using MongoDB Compass to edit documents on the fly. The documents I edit are almost always among the most recent ones created. Subsets of these documents represent an event that takes place on a date.

{
  "eventId": 1, // data about the event itself, such as date, is stored in another collection
  "title": "My title",
  "subtitle": "My subtitle",
  /* other fields... */
}

Ideally, when I click the Find button in Compass, it will list the documents in descending order of eventId, and ascending order of the entry's title and subtitle. I've created the index for this:

db.myCollection.createIndex({
    eventId: -1,
    title": 1,
    subtitle: 1
});

Is it possible to tell the MongoDB server to use this index when ordering documents, either during insertion (and then rebuilding the dataset that already exists), or at least to use it when returning results without sort/$orderby being specified?

If this is only possible at the time the collection is created, my dataset is small enough that creating a new one and copying over the data would not be an issue.

dx_over_dt
  • 13,240
  • 17
  • 54
  • 102

1 Answers1

0

Per the documentation:

Unless you specify the sort() method or use the $near operator, MongoDB does not guarantee the order of query results.

You can just sort it like this and it will use the index:

db.myCollection.find().sort({eventId: -1, title: 1, subtitle: 1})

To see if the index is really being used or not, try this:

db.myCollection.explain().find().sort({eventId: -1, title: 1, subtitle: 1})

In the output, look for "stage" : "IXSCAN" in the winningPlan. If it is there, then the query was performed using an index.

More info: https://docs.mongodb.com/manual/core/query-optimization/ https://docs.mongodb.com/manual/reference/explain-results/

Mikael Lirbank
  • 4,355
  • 2
  • 28
  • 25
  • I know that I can do that. That's not my question. I don't want to have to type in my sort every time I query the database to make edits. – dx_over_dt Jan 25 '19 at 21:31
  • Got it. The documents will be returned in "natural order", which means in the order they are stored on disk, unless you provide a sort criteria. – Mikael Lirbank Jan 25 '19 at 21:44
  • So my question is is there a way to inform the database which order should be "natural"? – dx_over_dt Jan 25 '19 at 22:09
  • 1
    I am pretty much 100% sure there is no way to set the "natural" order. The docs say: "The $natural parameter returns items according to their natural order within the database. This ordering is an internal implementation feature, and you should not rely on any particular structure within it." See: https://docs.mongodb.com/manual/reference/method/cursor.sort/#return-in-natural-order – Mikael Lirbank Jan 26 '19 at 00:04
  • See this thread as well: https://stackoverflow.com/questions/33018048/how-does-mongodb-order-their-docs-in-one-collection – Mikael Lirbank Jan 26 '19 at 00:04
  • 1
    You could file a feature request to the Compass team, asking them to add a default sort order field per collection. Or even just that they'd store the sort order you type in between sessions, so when you come back to a collection view the sort is still there. That would be a pretty nifty feature actually :) Sorry I couldn't be of more help! – Mikael Lirbank Jan 26 '19 at 00:08