MongoDB shell 中預設美化列印?


可以在遊標物件上呼叫 pretty() 函式,以在 MongoDB shell 中美化列印。語法如下 -

db.yourCollectionName.find().pretty();

為了理解此概念,讓我們建立一個包含文件的集合。查詢以用文件建立集合如下 -

>db.prettyDemo.insertOne({"ClientName":"Larry","ClientAge":27,"ClientFavoriteCountry":["US","UK"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a440de01f572ca0ccf5f2")
}
>db.prettyDemo.insertOne({"ClientName":"Mike","ClientAge":57,"ClientFavoriteCountry":["AUS","UK"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a4420e01f572ca0ccf5f3")
}

使用 find() 方法從集合中顯示所有文件。查詢如下 -

> db.prettyDemo.find();

輸出如下 -

{ "_id" : ObjectId("5c8a440de01f572ca0ccf5f2"), "ClientName" : "Larry", "ClientAge" : 27, "ClientFavoriteCountry" : [ "US", "UK" ] }
{ "_id" : ObjectId("5c8a4420e01f572ca0ccf5f3"), "ClientName" : "Mike", "ClientAge" : 57, "ClientFavoriteCountry" : [ "AUS", "UK" ] }

以下是呼叫 pretty() 函式的查詢 -

> db.prettyDemo.find().pretty();

輸出如下 -

{
   "_id" : ObjectId("5c8a440de01f572ca0ccf5f2"),
   "ClientName" : "Larry",
   "ClientAge" : 27,
   "ClientFavouriteCountry" : [
      "US",
      "UK"
   ]
}
{
   "_id" : ObjectId("5c8a4420e01f572ca0ccf5f3"),
   "ClientName" : "Mike",
   "ClientAge" : 57,
   "ClientFavoriteCountry" : [
      "AUS",
      "UK"
   ]
}

更新於: 2019-07-30

168 次瀏覽

開始您的 職業

透過完成課程取得認證

開始學習
廣告
© . All rights reserved.