davidmh
davidmh

Reputation: 1378

How to paginate output inside mongo shell

Is it possible to pipe results in to a pager from within a mongo shell?

The mysql cli equivalent would be:

mysql> pager less

Upvotes: 14

Views: 4277

Answers (3)

Mike Shauneu
Mike Shauneu

Reputation: 3289

You can try to use mongo --eval option. Something like:

mongo <db> --quiet --eval '<query>' | less

Upvotes: 4

Xavier Lamorlette
Xavier Lamorlette

Reputation: 1312

It doesn't seem possible, but you can output to a file, and then read your file with a pager in another terminal:

$ mongo | tee file.txt

See Printing Mongo query output to a file while in the mongo shell.

Upvotes: 2

Christian P
Christian P

Reputation: 12240

Mongo shell already paginates the results if the returned cursor is not assigned to a variable. From the documentation:

...in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. The mongo shell will prompt Type it to iterate another 20 times.

You can set the DBQuery.shellBatchSize attribute to change the number of iteration from the default value 20.

Upvotes: 3

Related Questions