14

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

The mysql cli equivalent would be:

mysql> pager less

davidmh
  • 1,368
  • 13
  • 24

3 Answers3

4

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

mongo <db> --quiet --eval '<query>' | less
Everett
  • 8,746
  • 5
  • 35
  • 49
Mike Shauneu
  • 3,201
  • 19
  • 21
3

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.

Christian P
  • 12,032
  • 6
  • 60
  • 71
  • 4
    Not quite what I'm looking for. This only paginates based on the number of results, even if the content overflows the screen height, plus I have to type "it" to move to the next set of records. eg. In mongo, searching for db.users.find().pretty() would overflow the screen height even with a DBQuery.shellBatchSize = 1 -> http://i.imgur.com/VwGJYqr.png with a mysql pager, the results get paged in relation to the screen height, regardless of a limit; so "select * from users\G" gives me -> http://i.imgur.com/fCsj6P9.png And you can see that I can even search inside the buffered results. – davidmh Apr 20 '14 at 14:35
  • @davidmh If I'm not mistaken MongoDB doesn't have what you want. You could probably do a script to do a manual format according to your wishes. – Christian P Apr 20 '14 at 19:34
2

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.

Community
  • 1
  • 1
Xavier Lamorlette
  • 1,152
  • 1
  • 12
  • 20