317
db.foo.find().limit(300)

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

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

will both print out very expanded view of each document instead of the 1-line version for find():

1
  • 2
    By default mongo shell print only first 20 documents, you can get next batch 20 documents by typing Type it in the shell. And so on. Feb 13, 2019 at 4:18

9 Answers 9

462
DBQuery.shellBatchSize = 300

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

5
  • 1
    Is there a way to make this persistent? Jun 21, 2016 at 12:20
  • 10
    @LukaszWiktor yes, you can create a $HOME/.mongorc.js file and put that shellBatchSize setting in there. mine has the query batch size setting and rs.slaveOk() enabled. it's usage is also slightly different when using --eval via commandline. see: docs.mongodb.com/manual/mongo/#mongorc-js-file Dec 3, 2016 at 17:56
  • 1
    the link is outdated, it contains no such information. Also, can I pass this as an environment variable?
    – phil294
    Dec 5, 2019 at 10:08
  • 2
    In case you want to run this from a command shell, you can do it like this: mongo --eval "DBQuery.shellBatchSize = 100; db.foo.find()"
    – Jelle
    Sep 29, 2020 at 8:06
  • 1
    A couple of answers below now mention that DBQuery.shellBatchSize is deprecated, and config.set("displayBatchSize", 300) should be used instead.
    – mwfearnley
    Jul 5 at 12:13
222

From the shell you can use:

db.collection.find().toArray()

to display all documents without having to use it.

5
  • 1
    This. If you just want them all in a shell so you can cut and paste, this is sufficient. Aug 4, 2016 at 10:43
  • This is exactly what did it for me. Truth be told I just needed a way to save all results from Robomongo output. Thank you! Jun 19, 2017 at 19:21
  • That's great, and it also let me pipe the result to jq :) Aug 20, 2017 at 12:28
  • 1
    Awesome in conjunction with Tee for a quick export: mongo | tee ./myOutput.txt
    – Metafaniel
    Jul 10, 2020 at 21:51
  • This is exactly what the question asker said they did not want to do, since it outputs the results differently.
    – M. Justin
    Jun 16 at 15:48
85

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.

3
  • 42
    oh it is really about how to print out all without using it Sep 14, 2010 at 1:37
  • 8
    The whole idea is NOT to use the iterator
    – Amir Kost
    Sep 18, 2016 at 16:45
  • 4
    I downvoted because this is what the OP wanted to avoid (which is the default behaviour).
    – crafter
    Aug 31, 2017 at 10:13
49

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.

3
  • 1
    I like this way becase it can be running in shell script ( mongo client with --eval )
    – Zheng Kai
    Sep 26, 2013 at 3:21
  • 1
    @ZhengKai if you're using a script and not in the shell then shellBatchSize is not relevant as your results won't be iterated over for you by the shell, you'll have to do it yourself. Nov 6, 2013 at 4:36
  • The tojson() was exactly what I was looking for to convert it from DBQuery thank you! Oct 26, 2015 at 17:10
17

With newer version of mongo shell (mongosh) use following syntax:

config.set("displayBatchSize", 300)

instead of depreciated:

DBQuery.shellBatchSize = 300

Future find() or aggregate() operations will only return 300 documents per cursor iteration.

7

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

6

DBQuery.shellBatchSize is deprecated and you need to use another command instead of this called config.set("displayBatchSize").

Example:

config.set("displayBatchSize", 300)

Configure Settings Using the API — MongoDB Shell

0

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/

-1
show dbs

use your database name in my case, I'm using - use smartbank then - show collections - just to check the document collections name. and finally, db. your collection name.find() or find({}) -

show dbs

use smartbank

show collections

db.users.find() or db.users.find({}) or db.users.find({_id: ObjectId("60c8823cbe9c1c21604f642b")}) or db.users.find({}).limit(20)

you can specify _id:ObjectId(write the document id here) to get the single document

or you can specify limit - db.users.find({}).limit(20)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.