db.collection.find() 方法是从集合中检索文档的 JavaScript 方法。db.collection.find() 方法将游标返回给结果;但是,在 mongo shell 中,如果返回的游标没有使用 var 关键字分配给变量,则游标将自动迭代最多 20 次,以打印与查询匹配的前 20 个文档。如果结果多余 20 个,mongo shell 将提示输入“it”再迭代20次。如下:
> db.phrase.find() { "_id" : ObjectId("5e63abda2c09b7744fb01b48"), "value" : "春暖花开" } { "_id" : ObjectId("5e63abda2c09b7744fb01b49"), "value" : "十字路口" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4a"), "value" : "千军万马" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4b"), "value" : "白手起家" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4c"), "value" : "张灯结彩" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4d"), "value" : "风和日丽" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4e"), "value" : "万里长城" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4f"), "value" : "人来人往" } { "_id" : ObjectId("5e63abda2c09b7744fb01b50"), "value" : "自由自在" } { "_id" : ObjectId("5e63abda2c09b7744fb01b51"), "value" : "瓜田李下" } { "_id" : ObjectId("5e63abda2c09b7744fb01b52"), "value" : "助人为乐" } { "_id" : ObjectId("5e63abda2c09b7744fb01b53"), "value" : "红男绿女" } { "_id" : ObjectId("5e63abda2c09b7744fb01b54"), "value" : "春风化雨" } { "_id" : ObjectId("5e63abda2c09b7744fb01b55"), "value" : "马到成功" } { "_id" : ObjectId("5e63abda2c09b7744fb01b56"), "value" : "拔苗助长" } { "_id" : ObjectId("5e63abda2c09b7744fb01b57"), "value" : "安居乐业" } { "_id" : ObjectId("5e63abda2c09b7744fb01b58"), "value" : "走马观花" } { "_id" : ObjectId("5e63abda2c09b7744fb01b59"), "value" : "念念不忘" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5a"), "value" : "落花流水" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5b"), "value" : "一往无前" } Type "it" for more > it { "_id" : ObjectId("5e63abda2c09b7744fb01b5c"), "value" : "落地生根" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5d"), "value" : "天罗地网" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5e"), "value" : "东山再起" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5f"), "value" : "一事无成" } { "_id" : ObjectId("5e63abda2c09b7744fb01b60"), "value" : "山清水秀" } { "_id" : ObjectId("5e63abda2c09b7744fb01b61"), "value" : "语重心长" } { "_id" : ObjectId("5e63abda2c09b7744fb01b62"), "value" : "别有洞天" } { "_id" : ObjectId("5e63abda2c09b7744fb01b63"), "value" : "水深火热" } { "_id" : ObjectId("5e63abda2c09b7744fb01b64"), "value" : "鸟语花香" } { "_id" : ObjectId("5e63abdb2c09b7744fb01b65"), "value" : "自以为是" } >
上面,第一次显示了20个文档,然后输入 “it”再次显示了剩余的文档。
我们可以通过改变 DBQuery.shellBatchSize 属性的值去设置批处理大小。实例:将批处理大小修改为5。
> DBQuery.shellBatchSize=5; 5 > db.phrase.find() { "_id" : ObjectId("5e63abda2c09b7744fb01b48"), "value" : "春暖花开" } { "_id" : ObjectId("5e63abda2c09b7744fb01b49"), "value" : "十字路口" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4a"), "value" : "千军万马" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4b"), "value" : "白手起家" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4c"), "value" : "张灯结彩" } Type "it" for more > it { "_id" : ObjectId("5e63abda2c09b7744fb01b4d"), "value" : "风和日丽" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4e"), "value" : "万里长城" } { "_id" : ObjectId("5e63abda2c09b7744fb01b4f"), "value" : "人来人往" } { "_id" : ObjectId("5e63abda2c09b7744fb01b50"), "value" : "自由自在" } { "_id" : ObjectId("5e63abda2c09b7744fb01b51"), "value" : "瓜田李下" } Type "it" for more > it { "_id" : ObjectId("5e63abda2c09b7744fb01b52"), "value" : "助人为乐" } { "_id" : ObjectId("5e63abda2c09b7744fb01b53"), "value" : "红男绿女" } { "_id" : ObjectId("5e63abda2c09b7744fb01b54"), "value" : "春风化雨" } { "_id" : ObjectId("5e63abda2c09b7744fb01b55"), "value" : "马到成功" } { "_id" : ObjectId("5e63abda2c09b7744fb01b56"), "value" : "拔苗助长" } Type "it" for more > it { "_id" : ObjectId("5e63abda2c09b7744fb01b57"), "value" : "安居乐业" } { "_id" : ObjectId("5e63abda2c09b7744fb01b58"), "value" : "走马观花" } { "_id" : ObjectId("5e63abda2c09b7744fb01b59"), "value" : "念念不忘" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5a"), "value" : "落花流水" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5b"), "value" : "一往无前" } Type "it" for more > it { "_id" : ObjectId("5e63abda2c09b7744fb01b5c"), "value" : "落地生根" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5d"), "value" : "天罗地网" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5e"), "value" : "东山再起" } { "_id" : ObjectId("5e63abda2c09b7744fb01b5f"), "value" : "一事无成" } { "_id" : ObjectId("5e63abda2c09b7744fb01b60"), "value" : "山清水秀" } Type "it" for more > it { "_id" : ObjectId("5e63abda2c09b7744fb01b61"), "value" : "语重心长" } { "_id" : ObjectId("5e63abda2c09b7744fb01b62"), "value" : "别有洞天" } { "_id" : ObjectId("5e63abda2c09b7744fb01b63"), "value" : "水深火热" } { "_id" : ObjectId("5e63abda2c09b7744fb01b64"), "value" : "鸟语花香" } { "_id" : ObjectId("5e63abdb2c09b7744fb01b65"), "value" : "自以为是" } > it no cursor