How to print out more than 20 items (documents) in MongoDB's shell?

前端 未结 6 1070
南笙
南笙 2020-11-28 00:22
db.foo.find().limit(300)

won\'t do it. It still prints out only 20 documents.

db.foo.find().toArray()
db.foo.find().forEach(printjs         


        
相关标签:
6条回答
  • 2020-11-28 01:11

    You can use it inside of the shell to iterate over the next 20 results. Just type it if you see "has more" and you will see the next 20 items.

    0 讨论(0)
  • 2020-11-28 01:11

    Could always do:

    db.foo.find().forEach(function(f){print(tojson(f, '', true));});
    

    To get that compact view.

    Also, I find it very useful to limit the fields returned by the find so:

    db.foo.find({},{name:1}).forEach(function(f){print(tojson(f, '', true));});
    

    which would return only the _id and name field from foo.

    0 讨论(0)
  • 2020-11-28 01:18

    I suggest you to have a ~/.mongorc.js file so you do not have to set the default size everytime.

     # execute in your terminal
     touch ~/.mongorc.js
     echo 'DBQuery.shellBatchSize = 100;' > ~/.mongorc.js
     # add one more line to always prettyprint the ouput
     echo 'DBQuery.prototype._prettyShell = true; ' >> ~/.mongorc.js
    

    To know more about what else you can do, I suggest you to look at this article: http://mo.github.io/2017/01/22/mongo-db-tips-and-tricks.html

    0 讨论(0)
  • 2020-11-28 01:18

    In the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, the cursor is automatically iterated to access up to the first 20 documents that match the query. You can set the DBQuery.shellBatchSize variable to change the number of automatically iterated documents.

    Reference - https://docs.mongodb.com/v3.2/reference/method/db.collection.find/

    0 讨论(0)
  • 2020-11-28 01:21

    From the shell if you want to show all results you could do db.collection.find().toArray() to get all results without it.

    0 讨论(0)
  • 2020-11-28 01:22

    DBQuery.shellBatchSize = 300

    will do.

    MongoDB Docs - Configure the mongo Shell - Change the mongo Shell Batch Size

    0 讨论(0)
提交回复
热议问题