@@ -552,3 +552,57 @@ func TestExportNoData(t *testing.T) {
552552 assert .Error (t , err , "export with --assertExists should fail on nonexistent collection" )
553553}
554554
555+ // TestExportPretty verifies that mongoexport --pretty --jsonArray produces
556+ // indented, parseable JSON with correct field values.
557+ func TestExportPretty (t * testing.T ) {
558+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
559+ log .SetWriter (io .Discard )
560+
561+ const dbName = "mongoexport_pretty_test"
562+ const collName = "source"
563+
564+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
565+ require .NoError (t , err )
566+ client , err := sessionProvider .GetSession ()
567+ require .NoError (t , err )
568+ t .Cleanup (func () {
569+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
570+ t .Errorf ("dropping test database: %v" , err )
571+ }
572+ })
573+
574+ _ , err = client .Database (dbName ).Collection (collName ).InsertMany (t .Context (), []any {
575+ bson.D {{"a" , int32 (1 )}},
576+ bson.D {{"a" , int32 (1 )}, {"b" , int32 (1 )}},
577+ bson.D {{"a" , int32 (1 )}, {"b" , int32 (2 )}, {"c" , int32 (3 )}},
578+ })
579+ require .NoError (t , err )
580+
581+ toolOptions , err := testutil .GetToolOptions ()
582+ require .NoError (t , err )
583+ toolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
584+ me , err := New (Options {
585+ ToolOptions : toolOptions ,
586+ OutputFormatOptions : & OutputFormatOptions {
587+ Type : "json" ,
588+ JSONFormat : "relaxed" ,
589+ JSONArray : true ,
590+ Pretty : true ,
591+ },
592+ InputOptions : & InputOptions {},
593+ })
594+ require .NoError (t , err )
595+ defer me .Close ()
596+
597+ var buf bytes.Buffer
598+ _ , err = me .Export (& buf )
599+ require .NoError (t , err )
600+
601+ var docs []map [string ]any
602+ require .NoError (t , json .Unmarshal (buf .Bytes (), & docs ), "output should be valid JSON array" )
603+ require .Len (t , docs , 3 , "should have 3 documents" )
604+ assert .Equal (t , float64 (1 ), docs [0 ]["a" ], "docs[0].a should be 1" )
605+ assert .Equal (t , float64 (1 ), docs [1 ]["b" ], "docs[1].b should be 1" )
606+ assert .Equal (t , float64 (2 ), docs [2 ]["b" ], "docs[2].b should be 2" )
607+ assert .Equal (t , float64 (3 ), docs [2 ]["c" ], "docs[2].c should be 3" )
608+ }
0 commit comments