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

allow delete to supply sort option

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Optimization

      Similar to SERVER-78140, expose the sort option in the delete command.

      Motivation

      Some drivers devs adding sort to update API (DRIVERS-2822) were surprised that sort was not available for delete:

      const collName = "foo";
      const coll = db[collName];
      // Can do single-document update with sort.
      {
          coll.drop();
          coll.insertMany([{ a: 1 }, { a: 2 }]);
          db.runCommand({
              update: collName,
              updates: [{ q: {}, u: { $inc: { a: 1 } }, multi: false, sort: { a: -1 } }]
          });
          assert(coll.findOne({ a: 3 }))
      }
      
      // Cannot do single-document delete with sort?
      {
          coll.drop();
          coll.insertMany([{ a: 1 }, { a: 2 }]);
          db.runCommand({
              delete: collName,
              deletes: [{ q: {}, limit: 1, sort: { a: -1 } }]
          });
          // Throws: MongoServerError: BSON field 'delete.deletes.sort' is an unknown field.
      }
      

      SERVER-78140 notes "multiple users have asked for this", but I am unsure if there is a similar user need for delete.

            Assignee:
            Unassigned Unassigned
            Reporter:
            kevin.albertson@mongodb.com Kevin Albertson
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: