定义
cursor.pretty()重要
mongosh 方法
本页介绍了
mongosh方法。这不是特定于语言的驾驶员(例如 Node.js)的文档。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
配置游标,以易于阅读的格式显示结果。
pretty()方法具有以下原型形式:db.collection.find(<query>).pretty()
行为
pretty()方法:
更改旧版 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" }