mongoDB findで表示される件数を変更する

mongoDB findで表示される件数を変更する

mongoDBで、findで表示される件数を変更する手順を記述してます。

環境

  • OS Ubuntu 20.04
  • MongoDB 5.0.5

手順

findで表示される件数を変更するには、「DBQuery.shellBatchSize」の値を指定します。

DBQuery.shellBatchSize = 値

まずは現在の値を確認してみます。

> DBQuery.shellBatchSize
20

デフォルトだと「20」になっているようです。「20」だと表示される件数は「20」件までとなります。

> db.foo.count()
30

> db.foo.find()
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fcd"), "name" : "itiro", "age" : 10, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fce"), "name" : "jiro", "age" : 20, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fcf"), "name" : "saburo", "age" : 30, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd0"), "name" : "jiro", "age" : 40, "gender" : "f" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd1"), "name" : "jiro", "age" : 50, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd2"), "name" : "jiro", "age" : 60, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd3"), "name" : "jiro", "age" : 70, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd4"), "name" : "jiro", "age" : 80, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd5"), "name" : "jiro", "age" : 90, "gender" : "x" }
{ "_id" : ObjectId("61c3d4ccb72dd2b156014fd6"), "name" : "jiro", "age" : 100, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fd7"), "name" : "itiro", "age" : 10, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fd8"), "name" : "jiro", "age" : 20, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fd9"), "name" : "saburo", "age" : 30, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fda"), "name" : "jiro", "age" : 40, "gender" : "f" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdb"), "name" : "jiro", "age" : 50, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdc"), "name" : "jiro", "age" : 60, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdd"), "name" : "jiro", "age" : 70, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fde"), "name" : "jiro", "age" : 80, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdf"), "name" : "jiro", "age" : 90, "gender" : "x" }
{ "_id" : ObjectId("61c3d4ceb72dd2b156014fe0"), "name" : "jiro", "age" : 100, "gender" : "x" }

これを「30」に変更してみます。

> DBQuery.shellBatchSize = 30
30

これで、表示されるデータが「30」に変更されたので、「30」表示されるようになります。

> db.foo.find()
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fcd"), "name" : "itiro", "age" : 10, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fce"), "name" : "jiro", "age" : 20, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fcf"), "name" : "saburo", "age" : 30, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd0"), "name" : "jiro", "age" : 40, "gender" : "f" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd1"), "name" : "jiro", "age" : 50, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd2"), "name" : "jiro", "age" : 60, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd3"), "name" : "jiro", "age" : 70, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd4"), "name" : "jiro", "age" : 80, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cbb72dd2b156014fd5"), "name" : "jiro", "age" : 90, "gender" : "x" }
{ "_id" : ObjectId("61c3d4ccb72dd2b156014fd6"), "name" : "jiro", "age" : 100, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fd7"), "name" : "itiro", "age" : 10, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fd8"), "name" : "jiro", "age" : 20, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fd9"), "name" : "saburo", "age" : 30, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fda"), "name" : "jiro", "age" : 40, "gender" : "f" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdb"), "name" : "jiro", "age" : 50, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdc"), "name" : "jiro", "age" : 60, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdd"), "name" : "jiro", "age" : 70, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fde"), "name" : "jiro", "age" : 80, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cdb72dd2b156014fdf"), "name" : "jiro", "age" : 90, "gender" : "x" }
{ "_id" : ObjectId("61c3d4ceb72dd2b156014fe0"), "name" : "jiro", "age" : 100, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe1"), "name" : "itiro", "age" : 10, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe2"), "name" : "jiro", "age" : 20, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe3"), "name" : "saburo", "age" : 30, "gender" : "m" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe4"), "name" : "jiro", "age" : 40, "gender" : "f" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe5"), "name" : "jiro", "age" : 50, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe6"), "name" : "jiro", "age" : 60, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe7"), "name" : "jiro", "age" : 70, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe8"), "name" : "jiro", "age" : 80, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fe9"), "name" : "jiro", "age" : 90, "gender" : "x" }
{ "_id" : ObjectId("61c3d4cfb72dd2b156014fea"), "name" : "jiro", "age" : 100, "gender" : "x" }