Import / Export
File format
The import / export process supports the following formats:
- JSON: each line contains a JSON document
- JSON Array: single line containing a JSON list, which contains multiple JSON documents
- Multi-line JSON: multiple lines that make up a single JSON document
- CSV: each line contains a comma-separated list of fields. See more details on CSV below.
- BSON: BSON documents written back-to-back (similar to ".bson" file format).
For CSV, the following applies:
- the first line is a header containing a delimited list of field names. The field names are non-quoted strings. For example: FIRSTNAME,LASTNAME,AGE
- subsequent lines contains a delimited list of values, each representing a document.
- each value is serialized/deserialized using a JSON parser (unless a template is used). Consequently strings should be enclosed in quotes, and other JSON native types are recognized properly. For example: "John","Doe",22,false,"blue"
Import options
The following options are supported:
- inputFile: the input file
- format: the file format
- continueOnError: whether to continue importing after an error occurs
- dropCollection: Drop the collection before importing
- upsert: Upsert values instead of insert
- upsertFields: Comma-separated list of fields to upsert by. If not specified, save() will be used
- bulk: If turned on, the bulk insert function of the driver will be used. It may be faster but more client memory will be used, and error reporting is not accurate.
Additionally the CSV format supports the following options:
- delimiter: Optional, the character for delimiting values
- quote: Optional, the character for quoting strings
- template: Optional, the document template
The template document can be used to model the document more precisely with nesting and advanced types. Each value in the document should contain either:
- a regular value. It will be passed as-is.
- a string of the form "$name/type". The "name" references the field in the CSV file. See below for a list of available types. If no type is specified, "JSON" is used.
Following are the available types:
- JSON: any JSON native type, including quoted strings
- String: non-quoted strings
- Long, Double, Integer: a numeric representation
- Boolean: true or false
- Date: Number of milliseconds since Epoch
For example the following CSV lines can be imported with no options:
Name,Address,City,State,Score
"Jane Doe","123 Main St","Whereverville","CA",9.5
"Jimmy Gil","555 Broadway Ave","New York","NY",3.4
You can use a template to import the same lines but with non-quoted strings:
Name,Address,City,State,Score
Jane Doe,123 Main St,Whereverville,CA,9.5
Jimmy Gil,555 Broadway Ave,New York,NY,3.4
The template would be:
{ name: "$Name/String", addr: "$Address/String", city: "$City/String", state: "$State/String", score: "$Score" }
Here is a more complex example of CSV data:
Number,Gender,GivenName,MiddleInitial,Surname,StreetAddress,City,State,ZipCode,Country,EmailAddress,Username,Password,TelephoneNumber,MothersMaiden,Birthday,CCType,CCNumber,CVV2,CCExpires,NationalID,UPS,Occupation,Company,Vehicle,Domain,BloodType,Pounds,Kilograms,FeetInches,Centimeters,GUID,Latitude,Longitude,LastUpdated
1,male,Anthony,J,Dacosta,2600 Rafe Lane,Jackson,MS,39201,US,AnthonyJDacosta@pookmail.com,Thadern,Wuithaiph7,662-718-2255,Whalen,3/25/1970,Visa,4485519808748821,260,6/2014,587-03-9892,1Z 9A9 W98 38 3679 711 2,Economist,Parade of Shoes,2003 Mitsubishi Montero,SanFranciscoAgency.com,B+,214.5,97.5,5' 9",175,0bbc614d-c665-4b4c-b566-2b08e5ffa9c7,32.368619,-90.183518,1378503941138
2,male,Jason,L,Saltsman,2729 Robinson Lane,Delaware,OH,43015,US,JasonLSaltsman@spambob.com,Witelf,phoo7kee0Yo,740-368-9998,Allen,8/6/1933,Visa,4716889560316146,737,1/2017,302-64-2796,1Z 765 372 96 5450 656 8,Technician,Druther's,1996 Toyota Cresta,ThousandPalm.com,O+,182.6,83.0,5' 6",167,8028febb-e027-4028-b27c-7e586279f7d8,40.294155,-83.002662,1378503962391
For this data we can use the following template that uses advanced types:
{
"firstName": "$GivenName/String",
"lastName": "$Surname/String",
"gender": "$Gender/String",
"address": {
"streetAddress": "$StreetAddress/String",
"city": "$City/String",
"state": "$State/String",
"zip": "$ZipCode/Integer"
},
"dateOfBirth": "$Birthday/String",
"location": [
"$Longitude/Double",
"$Latitude/Double"
],
"lastUpdated": "$LastUpdated/Date",
"static": "UMongo rocks!",
"static2": 1234
}
Export options
The following options are supported:
- outputFile: the output file
- format: the file format
- fields: Comma-separated list of fields to export, mandatory for CSV format
- continueOnError: whether to continue importing after an error occurs
Additionally the CSV format supports the following options:
- header: Optional, the CSV header line
- delimiter: Optional, the character for delimiting values