如何在 mongoexport 中美化导出的 MongoDB 文档
当您使用MongoDB导出文档时,您可以选择“美化”它们。mongoexport
我的意思是,文档不是在一长行上呈现,而是包括换行符、缩进等,以使其更易于阅读。
要在 中执行此操作,请使用该参数。mongoexport``--pretty
例
假设我们有一个包含以下文档的集合:posts
{ “_id” : 1, “title” : “Web”, “body” : “blah 1”, “tags” : [ “html”, “css”, “sql”, “xml” ] } { “_id” : 2, “title” : “Animals”, “body” : “blah blah 2”, “tags” : [ “cats”, “dogs” ] } { “_id” : 3, “title” : “Oceans”, “body” : “blah blah blah 3”, “author” : { “name” : “Bart Pitt”, “Email” : “[email protected]” } }
我们可以使用该实用程序将该集合导出到 JSON 文件。mongoexport
没有美化
首先,下面是一个_不使用_参数的示例:--pretty
mongoexport --db=krankykranes --collection=posts --out=posts.json
该代码不使用参数,因此生成的 JSON 文件如下所示:--pretty
{"_id":1.0,“title”:“Web”,“body”:“blah 1”,“tags”:[“html”,“css”,“sql”,“xml”]} {"_id":2.0,“title”:“Animals”,“body”:“blah blah 2”,“tags”:[“cats”,“dogs”]} {"_id":3.0,“title”:“Oceans”,“body”:“blah blah blah 3”,“author”:{“name”:“Bart Pitt”,“Email”:“[email protected]”}}
与美化
首先,下面是一个_使用该_参数的示例:--pretty
mongoexport --db=krankykranes --collection=posts --pretty --out=posts.json
该代码使用该参数,因此生成的 JSON 文件如下所示:--pretty
{ “_id”: 1.0, “title”: “Web”, “body”: “blah 1”, “tags”: [ “html”, “css”, “sql”, “xml” ] } { “_id”: 2.0, “title”: “Animals”, “body”: “blah blah 2”, “tags”: [ “cats”, “dogs” ] } { “_id”: 3.0, “title”: “Oceans”, “body”: “blah blah blah 3”, “author”: { “name”: “Bart Pitt”, “Email”: “[email protected]” } }
现在,更容易辨认出每个字段/值对,尤其是数组和嵌入的文档。