db.foo.find().limit(300)
不会这样做 . 它仍然只打印出20个文档 .
db.foo.find().toArray() db.foo.find().forEach(printjson)
将打印出每个文档的非常展开的视图,而不是 find() 的1行版本:
find()
可以随时做:
db.foo.find().forEach(function(f){print(tojson(f, '', true));});
获得紧凑的视图 .
另外,我发现限制find返回的字段非常有用:
db.foo.find({},{name:1}).forEach(function(f){print(tojson(f, '', true));});
这将只从foo返回_id和name字段 .
DBQuery.shellBatchSize = 300
会做 .
MongoDB Docs - Getting Started with the mongo Shell - Executing Queries
您可以在shell中使用 it 来迭代接下来的20个结果 . 如果您看到"has more",只需输入 it 即可看到接下来的20个项目 .
it
在mongo shell中,如果未使用var关键字将返回的游标分配给变量,则会自动迭代游标以访问与查询匹配的前20个文档 . 您可以设置DBQuery.shellBatchSize变量以更改自动迭代文档的数量 .
参考 - https://docs.mongodb.com/v3.2/reference/method/db.collection.find/
如果你想显示所有结果,你可以在shell中进行 db.collection.find().toArray() 以获得没有它的所有结果 .
db.collection.find().toArray()
我建议你有一个 ~/.mongorc.js 文件,这样你就不必每次都设置默认大小 .
~/.mongorc.js
# 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
要了解更多关于你还能做些什么的建议,我建议你看一下这篇文章:http://mo.github.io/2017/01/22/mongo-db-tips-and-tricks.html
6 回答
可以随时做:
获得紧凑的视图 .
另外,我发现限制find返回的字段非常有用:
这将只从foo返回_id和name字段 .
会做 .
MongoDB Docs - Getting Started with the mongo Shell - Executing Queries
您可以在shell中使用
it
来迭代接下来的20个结果 . 如果您看到"has more",只需输入it
即可看到接下来的20个项目 .在mongo shell中,如果未使用var关键字将返回的游标分配给变量,则会自动迭代游标以访问与查询匹配的前20个文档 . 您可以设置DBQuery.shellBatchSize变量以更改自动迭代文档的数量 .
参考 - https://docs.mongodb.com/v3.2/reference/method/db.collection.find/
如果你想显示所有结果,你可以在shell中进行
db.collection.find().toArray()
以获得没有它的所有结果 .我建议你有一个
~/.mongorc.js
文件,这样你就不必每次都设置默认大小 .要了解更多关于你还能做些什么的建议,我建议你看一下这篇文章:http://mo.github.io/2017/01/22/mongo-db-tips-and-tricks.html