db.foo.find().limit(300)

不会这么做的。它仍然只打印了20份文件。

db.foo.find().toArray()
db.foo.find().forEach(printjson)

将打印每个文档的扩展视图,而不是find()的一行版本:


您可以在shell中使用它来遍历接下来的20个结果。如果你看到“有更多”,就输入它,你会看到接下来的20项。

DBQuery.shellBatchSize = 300

MongoDB Docs - Configure mongo Shell - Change mongo Shell Batch Size

从shell中可以使用:

db.collection.find().toArray()

显示所有文档而不必使用它。

总是可以做:

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字段。

在mongo shell中,如果返回的游标没有使用var关键字分配给一个变量,游标将自动迭代以访问与查询匹配的前20个文档。可以设置DBQuery。shellBatchSize变量来更改自动迭代文档的数量。

参考资料- https://docs.mongodb.com/v3.2/reference/method/db.collection.find/

我建议你有一个~/.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

show dbs

使用数据库名称 就我而言,我用的是智能银行 然后-显示集合-检查文档集合名称。 最后是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)来获取单个文档

或者你可以指定limit - db.users.find({}).limit(20)

在mongo shell (mongosh)的新版本中,使用以下语法:

config.set("displayBatchSize", 300)

而不是贬值:

DBQuery.shellBatchSize = 300

将来的find()或aggregate()操作每次游标迭代只返回300个文档。