Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1026

Mongosh outputing in non-json parse-able format

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • No version
    • Affects Version/s: 1.1.0
    • Component/s: CLI Module
    • None
    • Needed
    • Hide

      It’s a good idea to include in the --eval docs that wrapping the result of the script in EJSON.stringify() is a good way to consistently get machine-parseable output from mongosh.

      Show
      It’s a good idea to include in the --eval docs that wrapping the result of the script in EJSON.stringify() is a good way to consistently get machine-parseable output from mongosh.

      Problem Statement/Rationale

      Output from --eval commands in mongosh are unable to be parsed by jq

      Steps to Reproduce

      I've included the commands I'm running in the "Additional notes" section

       

      Expected Results

      I expect the output of mongosh to match mongo, or at least be parse-able by jq

      Actual Results

      Parse exception when passed into jq

       

      Additional Notes

      Output is different between the 2 different shell applications. `mongo` is able to be parsed by `jq`, where mongosh will have the following error `parse error: Invalid numeric literal at line 2, column 7` (using `jq '.[]'` as only set of filtering still causes this parse error)

      $ mongosh --quiet -u <username> -p <pass> --host rs0/<host> --eval "rs.status().members.map(function(m) { return {{

      {'id':m._id, 'name':m.name, 'stateStr':m.stateStr}

      }}});"
      {{ [}}
        { id: 0, name: '<ip1>:27017', stateStr: 'PRIMARY' },
        { id: 1, name: '<ip2>:27017', stateStr: 'SECONDARY' },
      {{  { id: 2, name: '<ip3>:27017', stateStr: 'SECONDARY' }}}
      {{ ]}}
      {{ $ mongo --quiet -u <username> -p <pass> --host rs0/<host> --eval "rs.status().members.map(function(m) { return }}{{

      {'id':m._id, 'name':m.name, 'stateStr':m.stateStr}

      }}});"
      {{ [}}

        {

          "id" : 0,

          "name" : "<ip1>:27017",

          "stateStr" : "PRIMARY"

        }}}{{,

        {

          "id" : 1,

          "name" : "<ip2>:27017",

          "stateStr" : "SECONDARY"

        }}}{{,

        {

          "id" : 2,

          "name" : "<ip3>:27017",

          "stateStr" : "SECONDARY"

      {{  }}}

      ]

        1. mongosh_example.txt
          0.9 kB
          Crystal Fletcher

            Assignee:
            Unassigned Unassigned
            Reporter:
            crystal.fletcher@target.com Crystal Fletcher
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: