定义
cursor.pretty()重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
配置游标,以易于阅读的格式显示结果。
The
pretty()method has the following prototype form:db.collection.find(<query>).pretty()
行为
The pretty() method:
Does not change the output format in
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" }
By using cursor.pretty() you can set the cursor to return data in a format that is easier to read:
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" }