Database Manual / Reference / mongosh Methods / Cursors

cursor.pretty() (mongosh method方法)

Definition定义

cursor.pretty()

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Configures the cursor to display results in a format that is easy to read.配置游标以易于阅读的格式显示结果。

The pretty() method has the following prototype form:pretty()方法具有以下原型形式:

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

Behavior行为

The pretty() method:pretty()方法:

Examples示例

Consider the following document:考虑以下文件:

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"})

By default, db.collection.find() returns data in a dense format:默认情况下,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" }

By using cursor.pretty() you can set the cursor to return data in a format that is easier to read:通过使用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"
}