Mongosh json output

Hello All,

mongo outputs json in the following format…

    root@124d7cfb725d:/# mongo --eval "db.adminCommand('listDatabases')"
    MongoDB shell version v5.0.0
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("61ef35d1-7e31-43e1-87bb-e3f27405a1c1") }
    MongoDB server version: 5.0.0
    {
    	"databases" : [
    		{
    			"name" : "admin",
    			"sizeOnDisk" : NumberLong(40960),
    			"empty" : false
    		},
    		{
    			"name" : "config",
    			"sizeOnDisk" : NumberLong(12288),
    			"empty" : false
    		},
    		{
    			"name" : "local",
    			"sizeOnDisk" : NumberLong(40960),
    			"empty" : false
    		}
    	],
    	"totalSize" : NumberLong(94208),
    	"totalSizeMb" : NumberLong(0),
    	"ok" : 1
    }

while mongosh in 5.0 does the following…

    root@124d7cfb725d:/# mongosh --eval "db.adminCommand('listDatabases')"
    Current Mongosh Log ID:	60fab3516abd6eae1265d3d6
    Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
    Using MongoDB:		5.0.0
    Using Mongosh:		1.0.0

    For mongosh info see: https://docs.mongodb.com/mongodb-shell/

    ------
       The server generated these startup warnings when booting:
       2021-07-23T12:09:57.899+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
       2021-07-23T12:09:58.535+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
    ------

    {
      databases: [
        { name: 'admin', sizeOnDisk: Long("40960"), empty: false },
        { name: 'config', sizeOnDisk: Long("61440"), empty: false },
        { name: 'local', sizeOnDisk: Long("40960"), empty: false }
      ],
      totalSize: Long("143360"),
      totalSizeMb: Long("0"),
      ok: 1
    }

I get the following error with an existing application…

Expecting property name enclosed in double quotes: line 2 column 3 (char 4)

It would be nice if the outputted json was parsable without further processing or at least an option to allow this.

Cheers,

Rhys

1 Like

Hey Rhys, thanks for opening this issue! Can you please tell us what application are you using for parsing this output?

1 Like

Hello Sergey,

https://docs.ansible.com/ansible/latest/collections/community/mongodb/mongodb_shell_module.html

Using various Python versions. It’s just a simple json.loads(). With 5.0 I was already having to strip out the Extended JSON stuff when using the old mongo shell. The new mongosh shell has additional issues.

Rhys

Hey Rhys, if you are looking to parse output from mongosh programmatically, we are recommending to wrap the result with EJSON.stringify; for example:

$ /tmp/mongosh-1.0.1-linux-x64/bin/mongosh [...] --eval "EJSON.stringify(db.adminCommand('listDatabases'))"
Current Mongosh Log ID:	60fad826709a27c53c9cb992
Connecting to:		mongodb+srv://<credentials>@[...]/
Using MongoDB:		4.4.7
Using Mongosh:		1.0.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

{"databases":[{"name":"test","sizeOnDisk":589824,"empty":false},{"name":"admin","sizeOnDisk":335872,"empty":false},{"name":"local","sizeOnDisk":3412008960,"empty":false}],"totalSize":3573481472,"ok":1,"$clusterTime":{"clusterTime":{"$timestamp":{"t":1627052073,"i":1}},"signature":{"hash":{"$binary":{"base64":"0YqoHxcwf2/Vv8T5c+aUOj+bXoM=","subType":"00"}},"keyId":6928827991380197000}},"operationTime":{"$timestamp":{"t":1627052073,"i":1}}}

That’s always programmatically parseable. And if you want something prettier/more human-friendly, you can use EJSON.stringify(result, null, ' '), while still maintaining machine readability.

Does this help you?

3 Likes

Hello Anna,

With a bit of effort that’s probably workable. Consistent output across tools would probably prevent a lot of tooling bugs though. But c’est la vie.

Cheers,

R

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.