@@ -2201,3 +2201,74 @@ func TestRoundTripJSONArray(t *testing.T) {
22012201 assert .EqualValues (t , 1 , c , "document with _id %d should exist" , i )
22022202 }
22032203}
2204+
2205+ // TestRoundTripLimit verifies that mongoexport --limit restricts the number of
2206+ // exported documents, and that the correct documents are restored (from limit.js).
2207+ func TestRoundTripLimit (t * testing.T ) {
2208+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
2209+
2210+ const dbName = "mongoimport_roundtrip_limit_test"
2211+ const collName = "data"
2212+
2213+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
2214+ require .NoError (t , err )
2215+ client , err := sessionProvider .GetSession ()
2216+ require .NoError (t , err )
2217+ t .Cleanup (func () {
2218+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
2219+ t .Errorf ("dropping test database: %v" , err )
2220+ }
2221+ })
2222+
2223+ coll := client .Database (dbName ).Collection (collName )
2224+ docs := make ([]any , 50 )
2225+ for i := range 50 {
2226+ docs [i ] = bson.D {{"a" , int32 (i )}}
2227+ }
2228+ _ , err = coll .InsertMany (t .Context (), docs )
2229+ require .NoError (t , err )
2230+
2231+ exportToolOptions , err := testutil .GetToolOptions ()
2232+ require .NoError (t , err )
2233+ exportToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
2234+ me , err := mongoexport .New (mongoexport.Options {
2235+ ToolOptions : exportToolOptions ,
2236+ OutputFormatOptions : & mongoexport.OutputFormatOptions {
2237+ Type : "json" ,
2238+ JSONFormat : "canonical" ,
2239+ },
2240+ InputOptions : & mongoexport.InputOptions {Sort : "{a:1}" , Limit : 20 },
2241+ })
2242+ require .NoError (t , err )
2243+ defer me .Close ()
2244+ tmpFile , err := os .CreateTemp (t .TempDir (), "export-*.json" )
2245+ require .NoError (t , err )
2246+ n , err := me .Export (tmpFile )
2247+ require .NoError (t , err )
2248+ require .NoError (t , tmpFile .Close ())
2249+ assert .EqualValues (t , 20 , n , "should export exactly 20 documents" )
2250+
2251+ require .NoError (t , coll .Drop (t .Context ()))
2252+
2253+ importToolOptions , err := testutil .GetToolOptions ()
2254+ require .NoError (t , err )
2255+ importToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
2256+ mi , err := New (Options {
2257+ ToolOptions : importToolOptions ,
2258+ InputOptions : & InputOptions {File : tmpFile .Name (), ParseGrace : "stop" },
2259+ IngestOptions : & IngestOptions {},
2260+ })
2261+ require .NoError (t , err )
2262+ imported , _ , err := mi .ImportDocuments ()
2263+ require .NoError (t , err )
2264+ assert .EqualValues (t , 20 , imported , "should import all 20 exported documents" )
2265+
2266+ count , err := coll .CountDocuments (t .Context (), bson.D {})
2267+ require .NoError (t , err )
2268+ assert .EqualValues (t , 20 , count , "collection should have exactly 20 documents" )
2269+ for i := range int32 (20 ) {
2270+ c , err := coll .CountDocuments (t .Context (), bson.D {{"a" , i }})
2271+ require .NoError (t , err )
2272+ assert .EqualValues (t , 1 , c , "document with a=%d should exist (first 20 by sort)" , i )
2273+ }
2274+ }
0 commit comments