ExportXMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Won't Do
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Java Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Feature Request

      Provide a utility in the MongoDB Java Driver to convert BSON commands captured by CommandListener into human-readable and executable mongosh statements.

      Motivation

      When debugging MongoDB operations in Java applications, CommandStartedEvent#getCommand() provides the actual BSON command sent to MongoDB.

      However, developers currently need to manually convert these commands into native MongoDB Shell statements before they can reproduce and debug them in mongosh or GUI tools.

      For example, a captured command:

      {{{
      "find": "users",
      "filter": {
      "age":

      { "$gte": 18 }

      },
      "sort": {
      "age": -1
      },
      "limit": 10,
      "$db": "test"
      }}}

      Expected output:

      {{db.getSiblingDB("test")
      .getCollection("users")
      .find({ age:

      { $gte: 18 }

      })
      .sort({ age: -1 })
      .limit(10)}}

      Proposed API

      For example:

      String shell = MongoShellConverter.convert(commandStartedEvent);

      Alternatively, the utility could accept a database name, command name, and BsonDocument directly, allowing it to be reused independently of CommandListener.

      Expected Behavior

      • Support common commands such as find, aggregate, insert, update, delete, distinct, and findAndModify.
      • Generate native mongosh APIs such as find(), aggregate(), updateOne(), and deleteMany(), rather than simply wrapping BSON commands in db.runCommand().
      • Preserve BSON types such as ObjectId, Date, Decimal128, and binary values.
      • Preserve important operation options, including sorting, projection, collation, limits, and upsert behavior.
      • Correctly handle commands containing multiple write operations.
      • Explicitly indicate when a command cannot be faithfully converted or independently replayed.

      The goal is behavioral equivalence, not necessarily reconstructing the original Java Driver method calls.

      Use Cases

      1. Debugging actual MongoDB commands executed by Java applications.
      1. Copying queries directly into mongosh or MongoDB GUI tools.
      1. Improving MongoDB command logging and troubleshooting.
      1. Investigating queries generated by frameworks and ODM libraries.
      1. Reproducing and optimizing slow queries.

      Additional Notes

      This could be provided as an optional utility or separate module to avoid adding dependencies to the core driver.

      If this functionality is outside the scope of the Java Driver, guidance on an appropriate existing tool or implementation location would also be appreciated.

            Assignee:
            Rishit Bhatia
            Reporter:
            MongoPlus N/A (EXT)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: