Search code examples
mongodbpretty-print

Set indentation level for pretty-printing in mongo shell


Is there a way to set the indentation level of MongoDB's pretty shell command?

Sometimes, deeply nested objects need to be wrapped when pretty-printing. While modules such as Python's pprint have a setting for the indentation level, MongoDB's pretty API does not seem to provide this.

Is there a workaround (any way to set the indentation of pretty-printing)?


Solution

  • As the mongo shell is a JavaScript shell, you can use its pretty-printing functionality. For example

    JSON.stringify(db.runs.find()[0], null, 2)
    

    The third parameter of JSON.stringify

    indicates the number of space characters to use as white space;