316
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():

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 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. – roottraveller Feb 13 '19 at 04:18

9 Answers9

462
DBQuery.shellBatchSize = 300

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

hnwoh
  • 128
  • 1
  • 10
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Is there a way to make this persistent? – Lukasz Wiktor Jun 21 '16 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: https://docs.mongodb.com/manual/mongo/#mongorc-js-file – matias elgart Dec 03 '16 at 17:56
  • 1
    the link is outdated, it contains no such information. Also, can I pass this as an environment variable? – phil294 Dec 05 '19 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 '20 at 08:06
  • A couple of answers below now mention that `DBQuery.shellBatchSize` is deprecated, and `config.set("displayBatchSize", 300)` should be used instead. – mwfearnley Jul 05 '23 at 12:13
222

From the shell you can use:

db.collection.find().toArray()

to display all documents without having to use it.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Sridhar
  • 2,530
  • 1
  • 14
  • 3
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.

halfdan
  • 33,545
  • 8
  • 78
  • 87
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.

Wilfred Knievel
  • 3,225
  • 1
  • 26
  • 33
16

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.

seven
  • 2,388
  • 26
  • 28
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

Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
4

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/

Harsh Raj
  • 240
  • 1
  • 11
-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