Is there a way to 'pretty' print MongoDB shell output to a file?

前端 未结 10 1953
余生分开走
余生分开走 2020-11-27 09:38

Specifically, I want to print the results of a mongodb find() to a file. The JSON object is too large so I\'m unable to view the entire object with the shell wi

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 10:23

    Put your query (e.g. db.someCollection.find().pretty()) to a javascript file, let's say query.js. Then run it in your operating system's shell using command:

    mongo yourDb < query.js > outputFile

    Query result will be in the file named 'outputFile'.

    By default Mongo prints out first 20 documents IIRC. If you want more you can define new value to batch size in Mongo shell, e.g.

    DBQuery.shellBatchSize = 100.

提交回复
热议问题