mongoDB 常時結果を整形(pretty)に変更する

mongoDB 常時結果を整形(pretty)に変更する

mongoDBで、常時結果を整形(pretty)に変更する手順を記述してます。「DBQuery.prototype._prettyShell」を「true」に設定します。mongo6では存在しません。

環境

  • OS Ubuntu 20.04
  • MongoDB 5.0.5

手順

常時結果を整形(pretty)に変更するには、「DBQuery.prototype._prettyShell」に「true」を指定します。

DBQuery.prototype._prettyShell = true

※mongo6の場合は実行するとエラーになります。

> DBQuery.prototype._prettyShell = true
TypeError: Cannot set properties of undefined (setting '_prettyShell')

「true」に設定してみます。

> DBQuery.prototype._prettyShell = true
true

「true」に設定すると「pretty()」を指定しなくても、整形されていることが確認できます。

> db.foo.find()

{
        "_id" : ObjectId("61c3d6b0b72dd2b156014feb"),
        "name" : "itiro",
        "age" : 10,
        "gender" : "m"
}
{
        "_id" : ObjectId("61c3d6bcb72dd2b156014fec"),
        "name" : "jiro",
        "age" : 20,
        "gender" : "m"
}

元に戻す場合は「false」を指定します。

> DBQuery.prototype._prettyShell = false
false

> db.foo.find()
{ "_id" : ObjectId("61c3d6b0b72dd2b156014feb"), "name" : "itiro", "age" : 10, "gender" : "m" }
{ "_id" : ObjectId("61c3d6bcb72dd2b156014fec"), "name" : "jiro", "age" : 20, "gender" : "m" }

mongo6の場合は、デフォルトで以下のように出力されます。

[
  {
    _id: ObjectId("6364ae7f06a29c57f191d1b7"),
    name: 'itiro',
    age: 10,
    gender: 'm'
  },
  {
    _id: ObjectId("6364ae7f06a29c57f191d1b8"),
    name: 'jiro',
    age: 20,
    gender: 'm'
  },
  {
    _id: ObjectId("6364ae7f06a29c57f191d1b9"),
    name: 'saburo',
    age: 30,
    gender: 'm'
  },
  {
    _id: ObjectId("6364ae7f06a29c57f191d1ba"),
    name: 'jiro',
    age: 25,
    gender: 'f'
  },
  {
    _id: ObjectId("6364ae7f06a29c57f191d1bb"),
    name: 'jiro',
    age: 30,
    gender: 'x'
  }
]