ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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.

X
Xavier Guihot
DBQuery.shellBatchSize = 300

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


Is there a way to make this persistent?
@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
the link is outdated, it contains no such information. Also, can I pass this as an environment variable?
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()"
X
Xavier Guihot

From the shell you can use:

db.collection.find().toArray()

to display all documents without having to use it.


This. If you just want them all in a shell so you can cut and paste, this is sufficient.
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!
That's great, and it also let me pipe the result to jq :)
Awesome in conjunction with Tee for a quick export: mongo | tee ./myOutput.txt
h
halfdan

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.


oh it is really about how to print out all without using it
Thanks halfdan, you're twice your username says you are!
The whole idea is NOT to use the iterator
I downvoted because this is what the OP wanted to avoid (which is the default behaviour).
W
Wilfred Knievel

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.


I like this way becase it can be running in shell script ( mongo client with --eval )
@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.
The tojson() was exactly what I was looking for to convert it from DBQuery thank you!
S
Sahith Vibudhi

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


s
seven

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.


H
Harsh Raj

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/


L
Lutfor Rahman
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)

https://i.stack.imgur.com/KwVJE.png