Skip to main content

What is Smuggler

Smuggler gives you the ability to export or import data from or to a database using JSON format. It is exposed via the DocumentStore.Smuggler property.

ForDatabase

By default, the DocumentStore.Smuggler works on the default document store database from the DocumentStore.Database property.

In order to switch it to a different database use the .ForDatabase method.

var northwindSmuggler = store
.Smuggler
.ForDatabase("Northwind");

Export

Syntax

Task<Operation> ExportAsync(
DatabaseSmugglerExportOptions options,
DatabaseSmuggler toDatabase,
CancellationToken token = default(CancellationToken));

Task<Operation> ExportAsync(
DatabaseSmugglerExportOptions options,
string toFile,
CancellationToken token = default(CancellationToken));
Parameters
optionsDatabaseSmugglerExportOptionsOptions that will be used during the export. Read more here.
toDatabaseDatabaseSmugglerDatabaseSmuggler instance used as a destination
toFilestringPath to a file where exported data will be written
tokenCancellationTokenToken used to cancel the operation
Return Value
OperationInstance of Operation class which gives you an ability to wait for the operation to complete and subscribe to operation progress events

DatabaseSmugglerExportOptions

Parameters
CollectionsList<string>List of specific collections to export. If empty, then all collections will be exported. Default: empty
OperateOnTypesDatabaseItemTypeIndicates what should be exported. Default: Indexes, Documents, RevisionDocuments, Conflicts, DatabaseRecord, Identities, CompareExchange
IncludeExpiredboolShould expired documents be included in the export. Default: true
RemoveAnalyzersboolShould analyzers be removed from Indexes. Default: false
TransformScriptstringJavaScript-based script applied to every exported document. Read more here.
MaxStepsForTransformScriptintMaximum number of steps that transform script can process before failing. Default: 10000

Example

// export only Indexes and Documents to a given file
var exportOperation = await store
.Smuggler
.ExportAsync(
new DatabaseSmugglerExportOptions
{
OperateOnTypes = DatabaseItemType.Indexes
| DatabaseItemType.Documents
},
@"C:\ravendb-exports\Northwind.ravendbdump",
token);

await exportOperation.WaitForCompletionAsync();

Import

Syntax

Task<Operation> ImportAsync(
DatabaseSmugglerImportOptions options,
Stream stream,
CancellationToken token = default(CancellationToken));

Task<Operation> ImportAsync(
DatabaseSmugglerImportOptions options,
string fromFile,
CancellationToken token = default(CancellationToken));
Parameters
optionsDatabaseSmugglerImportOptionsOptions that will be used during the import. Read more here.
streamStreamStream with data to import
fromFilestringPath to a file from which data will be imported
tokenCancellationTokenToken used to cancel the operation
Return Value
OperationInstance of Operation-class which gives you an ability to wait for the operation to complete and subscribe to operation progress events

DatabaseSmugglerImportOptions

Parameters
CollectionsList<string>List specific of collections to import. If empty then all collections will be imported. Default: empty
OperateOnTypesDatabaseItemTypeIndicates what should be imported. Default: Indexes, Documents, RevisionDocuments, Conflicts, DatabaseRecord, Identities, CompareExchange
IncludeExpiredboolShould expired documents be imported. Default: true
RemoveAnalyzersboolShould analyzers be removed from Indexes. Default: false
TransformScriptstringJavaScript-based script applied to every imported document. Read more here.
MaxStepsForTransformScriptintMaximum number of steps that transform script can process before failing. Default: 10000

Example

// import only Documents from a given file
var importOperation = await store
.Smuggler
.ImportAsync(
new DatabaseSmugglerImportOptions
{
OperateOnTypes = DatabaseItemType.Documents
},
// import the .ravendbdump file that you exported (i.e. in the export example above)
@"C:\ravendb-exports\Northwind.ravendbdump",
token);

await importOperation.WaitForCompletionAsync();

TransformScript

TransformScript exposes the ability to modify or even filter-out the document during the import and export process using the provided JavaScript.

Underneath the JavaScript engine is exactly the same as used for patching operations giving you identical syntax and capabilities with additional ability to filter out documents by throwing a 'skip' exception.

var id = this['@metadata']['@id'];
if (id === 'orders/999-A')
throw 'skip'; // filter-out

this.Freight = 15.3;