mongodb 如何在MongoDB的shell中打印出20多个项目(文档)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3705517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print out more than 20 items (documents) in MongoDB's shell?
提问by nonopolarity
db.foo.find().limit(300)
won't do it. It still prints out only 20 documents.
不会做。它仍然只打印出 20 个文档。
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()
:
都将打印出每个文档的非常扩展的视图,而不是以下内容的 1 行版本find()
:
回答by Thilo
DBQuery.shellBatchSize = 300
DBQuery.shellBatchSize = 300
will do.
会做。
MongoDB Docs - Configure the mongo Shell - Change the mongo Shell Batch Size
回答by Sridhar
From the shell if you want to show all results you could do db.collection.find().toArray()
to get all results without it.
从 shell 中如果你想显示所有结果,你可以在db.collection.find().toArray()
没有它的情况下获得所有结果。
回答by 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.
您可以使用it
shell 内部来迭代接下来的 20 个结果。it
如果您看到“还有更多”,只需输入,您就会看到接下来的 20 个项目。
回答by 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:
此外,我发现限制 find 返回的字段非常有用:
db.foo.find({},{name:1}).forEach(function(f){print(tojson(f, '', true));});
which would return only the _id and name field from foo.
这将仅从 foo 返回 _id 和 name 字段。
回答by Sahith Vibudhi
I suggest you to have a ~/.mongorc.js
file so you do not have to set the default size everytime.
我建议你有一个~/.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
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
要了解更多关于你还能做什么,我建议你看这篇文章:http: //mo.github.io/2017/01/22/mongo-db-tips-and-tricks.html
回答by 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.
在 mongo shell 中,如果返回的游标未使用 var 关键字分配给变量,则游标会自动迭代以访问与查询匹配的前 20 个文档。您可以设置 DBQuery.shellBatchSize 变量来更改自动迭代文档的数量。
Reference - https://docs.mongodb.com/v3.2/reference/method/db.collection.find/
参考 - https://docs.mongodb.com/v3.2/reference/method/db.collection.find/