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

Feature request: Numerically-ordered fields / index

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • ALL

      How are you using Mongo? What version of the server and driver are you using?

      MongoDB 5.x using in a Ruby application

      What is the feature/improvement you would like?

      I would like MongoDB to add better support for ordered lists across documents in a collection. This feature would allow the user to designate a field such as "position", and the DB would ensure the values of that field across documents remain in a monotonically-increasing integer sequence 0, 1, 2, 3, ... N.

      I am the maintainer of a Ruby library called "Mongoid Orderable". This library makes numerically ordered field across documents. I'd like to ask MongoDB to investigate moving the functionality of this library to the server.

      What use case would this feature/improvement enable?

      This feature would be useful in a wide-variety of real world applications; ordered lists are extremely common. In my app (standard SaaS related to restaurant management) I have at least 20 instances of such ordered lists.

      Suggested implementation

      I propose to do this using a new "ordered" index type.

      For example, suppose my application has many Car documents, and I want to maintain the "position" of the Cars in a consistent order for each Dealer.

      I add the following index to the Car collection:

      Cars -> createIndex({ dealer_id: 1, position: 1 }, { ordered: 1 })

      Then in my Car collection, I see:

      Car A -> dealer_id: xxx, position: 0
      Car B -> dealer_id: xxx, position: 1
      Car C -> dealer_id: xxx, position: 2
      Car D -> dealer_id: yyy, position: 0
      Car E -> dealer_id: yyy, position: 1
      

      Due to this special "ordered" index, the position field will always maintain a zero-based ordered value for the given scope. If I modify any member, this ordering is preserved:

      # Change Car C position
      Car C -> updateOne(position: 1)
      
      Car A -> dealer_id: xxx, position: 0
      Car B -> dealer_id: xxx, position: 2 <-- updated
      Car C -> dealer_id: xxx, position: 1 <-- updated
      Car D -> dealer_id: yyy, position: 0
      Car E -> dealer_id: yyy, position: 1
      
      # Change Car C dealer_id
      Car C -> updateOne(dealer_id: yyy)
      
      Car A -> dealer_id: xxx, position: 0
      Car B -> dealer_id: xxx, position: 1 <-- updated
      Car C -> dealer_id: yyy, position: 1 <-- updated
      Car D -> dealer_id: yyy, position: 0
      Car E -> dealer_id: yyy, position: 2 <-- updated
      
      # Change Car C position to be a very large value
      Car C -> updateOne(position: 999)
      
      Car A -> dealer_id: xxx, position: 0
      Car B -> dealer_id: xxx, position: 1
      Car C -> dealer_id: yyy, position: 2 <-- updated, stays in order
      Car D -> dealer_id: yyy, position: 0
      Car E -> dealer_id: yyy, position: 1 <-- updated
      
      # Change Car C position to a non-numeric value
      Car C -> updateOne(position: "foo")
      # update is ignored; nothing changes

      This feature should work with partial filter expressions, etc.

            Assignee:
            chris.kelly@mongodb.com Chris Kelly
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: