Docs 菜单

Docs 主页开发应用程序MongoDB Manual

cursor.pretty()

在此页面上

  • 定义
  • 行为
  • 举例
cursor.pretty()

重要

mongosh 方法

本页介绍了 mongosh方法。这不是特定于语言的驱动程序(例如 Node.js)的文档。

对于 MongoDB API 驱动程序,请参阅特定语言的MongoDB 驱动程序文档。

配置游标,以易于阅读的格式显示结果。

pretty()方法具有以下原型形式:

db.collection.find(<query>).pretty()

pretty()方法:

请考虑以下文档:

db.books.insertOne({
"_id" : ObjectId("54f612b6029b47909a90ce8d"),
"title" : "A Tale of Two Cities",
"text" : "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...",
"authorship" : "Charles Dickens"})

默认情况下,db.collection.find() 以密集格式返回数据:

db.books.find()
{ "_id" : ObjectId("54f612b6029b47909a90ce8d"), "title" : "A Tale of Two Cities", "text" : "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...", "authorship" : "Charles Dickens" }

通过使用cursor.pretty() ,可以将游标设置为以更易于阅读的格式返回数据:

db.books.find().pretty()
{
"_id" : ObjectId("54f612b6029b47909a90ce8d"),
"title" : "A Tale of Two Cities",
"text" : "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...",
"authorship" : "Charles Dickens"
}
← cursor.objsLeftInBatch()

在此页面上