dotConnect for MongoDB Documentation
On this page
    Bulk Data Loading
    On this page

    The MongoDBLoader component is used for bulk uploading a large number of records to MongoDB.

    To start uploading data, create a MongoDBLoader component. Then you may tweak its BatchSize property. The BatchSize property determines the maximum number of records in a batch (1000 by default). After this, you need to specify columns to load data. You may do it either manually, using the Columns property, or you may use the CreateColumns method to retrieve table column information from the table specified in the TableName property. Then call the Open method and use the SetValue method to set values for the row fields. To switch to the next row, call the NextRow method.

    After you have finished loading data, call the Close method. This method sends the last batch to the server.

    The target collection must exist unless the Create Collection On Insert connection string parameter is set to True. If a loaded field doesn't exist in the collection, MongoDBLoader writes it when the Allow Add Fields connection string parameter is set to True (default).

    The following example demonstrates how to use the MongoDBLoader class.

        using (var connection = new MongoDBConnection("Host=localhost;Database=sample;User Id=user;Password=pwd;License Key=***")) {
            connection.Open();
    
            MongoDBLoader loader = new MongoDBLoader("customers", connection);
            loader.Columns.Add(new MongoDBLoaderColumn("name", MongoDBType.String));
    
            try {
                loader.Open();
                for (int i = 1; i <= 100; i++) {
                    loader.SetValue("name", "Test " + i);
                    loader.NextRow();
                }
                loader.Close();
            }
            catch (MongoDBException ex) {
    
                // process exception
            }
        }
    
    
    
        Using connection As New MongoDBConnection("Host=localhost;Database=sample;User Id=user;Password=pwd;License Key=***")
            connection.Open()
    
            Dim loader As New MongoDBLoader("customers", connection)
            loader.Columns.Add(New MongoDBLoaderColumn("name", MongoDBType.String))
    
            Try
                loader.Open()
                For i As Integer = 1 To 100
                    loader.SetValue("name", "Test " & i)
                    loader.NextRow()
                Next
                loader.Close()
    
            Catch ex As MongoDBException
                'process exception
    
            End Try
        End Using