Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-7528

Order of responses to a MongoDB $in query

    • Query Execution
    • Fully Compatible

      I have a use-case where I am querying an array of ObjectIDs. For example:

      db.things.find({
      _id:

      { $in: [ ObjectID(...), ObjectID(...), ObjectID(...), ObjectID(...) ] }

      })

      I'd like the results to be ordered by `_id.$in`. Right now I'm resorting to client-side sorting, which kind of sucks since it requires converting between ObjectIDs and strings among other hacks.

      A special sorting option like this would be awesome (similar to $natural):

      db.things.find({}).sort({
      $in:

      { '_ids' }

      })

      Although the attribute being `$in`ed need not be `_id`, it should be unique (and thus indexed).

      Some related questions on StackOverflow:

      http://stackoverflow.com/questions/3142260/order-of-responses-to-mongodb-in-query
      http://stackoverflow.com/questions/11839515/comparing-and-sorting-mongodb-objectids-in-node-convert-to-string

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            jonathanong Jonathan Richard Ong
            Votes:
            27 Vote for this issue
            Watchers:
            25 Start watching this issue

              Created:
              Updated: