db.foo.find().limit(300)
不会的。它仍然只打印出 20 个文档。
db.foo.find().toArray()
db.foo.find().forEach(printjson)
都将打印出每个文档的扩展视图,而不是 find()
的 1 行版本:
Type it
来获取下一批 20 个文档。等等。
DBQuery.shellBatchSize = 300
MongoDB Docs - Configure the mongo Shell - Change the mongo Shell Batch Size
在 shell 中,您可以使用:
db.collection.find().toArray()
无需使用 it
即可显示所有文档。
mongo | tee ./myOutput.txt
您可以在 shell 中使用 it
来迭代接下来的 20 个结果。如果您看到“有更多”,只需输入 it
,您就会看到接下来的 20 项。
it
的情况下打印所有内容
总能做到:
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 字段。
--eval
的 mongo 客户端)
我建议您有一个 ~/.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
使用较新版本的 mongo shell (mongosh) 使用以下语法:
config.set("displayBatchSize", 300)
而不是折旧:
DBQuery.shellBatchSize = 300
未来的 find() 或 aggregate() 操作每次游标迭代只会返回 300 个文档。
在 mongo shell 中,如果返回的游标未使用 var 关键字分配给变量,则游标会自动迭代以访问最多与查询匹配的前 20 个文档。您可以设置 DBQuery.shellBatchSize 变量来更改自动迭代文档的数量。
参考 - https://docs.mongodb.com/v3.2/reference/method/db.collection.find/
show dbs
在我的情况下使用您的数据库名称,我使用 - use smartbank
然后 - show collections
- 只是为了检查文档集合名称。最后,db.
您的收藏名称.find()
或 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)
您可以指定 _id:ObjectId
(在此处写入文档 ID)以获取单个文档
或者您可以指定限制 - db.users.find({}).limit(20)
https://i.stack.imgur.com/KwVJE.png
不定期副业成功案例分享
mongo --eval "DBQuery.shellBatchSize = 100; db.foo.find()"