Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

cursor.pretty()

在此页面上

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

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是特定于语言的驱动程序(例如 Node.js)的文档。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

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

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

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

pretty() 方法:

  • 不改变 mongosh 中的输出格式。

  • 更改旧版 mongo shell 中的输出格式。

请考虑以下文档:

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

在此页面上