EJSON output from mongosh

Is there a way to output the EJSON.stringify format on queries once already running the mongosh? For

You can use EJSON.stringify() in the regular mongosh prompt as well on the result of .toArray(), e.g. EJSON.stringify(db.test.find().toArray(), null, 2) is totally fine.

but I’ve noticed that certain fields lose their data type descriptors

You might be looking for relaxed: false:

test> EJSON.stringify(db.longtest.find().toArray(), null, 2, { relaxed: false })
[
  {
    "_id": {
      "$oid": "60ca0404216deba577090bc4"
    },
    "l": {
      "$numberLong": "1"
    }
  },
  {
    "_id": {
      "$oid": "621767e082be3efa32ef43e0"
    },
    "l": {
      "$numberLong": "9007199254740993"
    }
  }
]

(The null, 2 bits here are just for prettier, more human-readable output by adding 2 spaces indentation.)

3 Likes