Documentation ¶
Overview ¶
Package mongoexport produces a JSON or CSV export of data stored in a MongoDB instance.
Index ¶
Constants ¶
const ( CSV = "csv" JSON = "json" )
Output types supported by mongoexport.
Variables ¶
var Usage = `` /* 147-byte string literal not displayed */
Functions ¶
This section is empty.
Types ¶
type CSVExportOutput ¶
type CSVExportOutput struct { // Fields is a list of field names in the bson documents to be exported. // A field can also use dot-delimited modifiers to address nested structures, // for example "location.city" or "addresses.0". Fields []string // NumExported maintains a running total of the number of documents written. NumExported int64 // contains filtered or unexported fields }
CSVExportOutput is an implementation of ExportOutput that writes documents to the output in CSV format.
func NewCSVExportOutput ¶
func NewCSVExportOutput(fields []string, out io.Writer) *CSVExportOutput
NewCSVExportOutput returns a CSVExportOutput configured to write output to the given io.Writer, extracting the specified fields only.
func (*CSVExportOutput) ExportDocument ¶
func (csvExporter *CSVExportOutput) ExportDocument(document bson.M) error
ExportDocument writes a line to output with the CSV representation of a document.
func (*CSVExportOutput) Flush ¶
func (csvExporter *CSVExportOutput) Flush() error
Flush writes any pending data to the underlying I/O stream.
func (*CSVExportOutput) WriteFooter ¶
func (csvExporter *CSVExportOutput) WriteFooter() error
WriteFooter is a no-op for CSV export formats.
func (*CSVExportOutput) WriteHeader ¶
func (csvExporter *CSVExportOutput) WriteHeader() error
WriteHeader writes a comma-delimited list of fields as the output header row.
type ExportOutput ¶
type ExportOutput interface { // WriteHeader outputs any pre-record headers that are written once // per output file. WriteHeader() error // WriteRecord writes the given document to the given io.Writer according to // the format supported by the underlying ExportOutput implementation. ExportDocument(bson.M) error // output file. WriteFooter() error // Flush writes any pending data to the underlying I/O stream. Flush() error }
ExportOutput is an interface that specifies how a document should be formatted and written to an output stream.
type InputOptions ¶
type InputOptions struct { Query string `long:"query" short:"q" description:"query filter, as a JSON string, e.g., '{x:{$gt:1}}'"` SlaveOk bool `long:"slaveOk" short:"k" description:"allow secondary reads if available (default true)" default:"true" default-mask:"-"` ForceTableScan bool `long:"forceTableScan" description:"force a table scan (do not use $snapshot)"` Skip int `long:"skip" description:"number of documents to skip"` Limit int `long:"limit" description:"limit the number of documents to export"` Sort string `long:"sort" description:"sort order, as a JSON string, e.g. '{x:1}'"` }
InputOptions defines the set of options to use in retrieving data from the server.
func (*InputOptions) Name ¶
func (*InputOptions) Name() string
Name returns a human-readable group name for input options.
type JSONExportOutput ¶
type JSONExportOutput struct { // ArrayOutput when set to true indicates that the output should be written // as a JSON array, where each document is an element in the array. ArrayOutput bool // Pretty when set to true indicates that the output will be written in pretty mode. PrettyOutput bool Encoder *json.Encoder Out io.Writer NumExported int64 }
JSONExportOutput is an implementation of ExportOutput that writes documents to the output in JSON format.
func NewJSONExportOutput ¶
func NewJSONExportOutput(arrayOutput bool, prettyOutput bool, out io.Writer) *JSONExportOutput
NewJSONExportOutput creates a new JSONExportOutput in array mode if specified, configured to write data to the given io.Writer.
func (*JSONExportOutput) ExportDocument ¶
func (jsonExporter *JSONExportOutput) ExportDocument(document bson.M) error
ExportDocument converts the given document to extended JSON, and writes it to the output.
func (*JSONExportOutput) Flush ¶
func (jsonExporter *JSONExportOutput) Flush() error
Flush is a no-op for JSON export formats.
func (*JSONExportOutput) WriteFooter ¶
func (jsonExporter *JSONExportOutput) WriteFooter() error
WriteFooter writes the closing square bracket if in array mode, otherwise it behaves as a no-op.
func (*JSONExportOutput) WriteHeader ¶
func (jsonExporter *JSONExportOutput) WriteHeader() error
WriteHeader writes the opening square bracket if in array mode, otherwise it behaves as a no-op.
type MongoExport ¶
type MongoExport struct { // generic mongo tool options ToolOptions options.ToolOptions // OutputOpts controls options for how the exported data should be formatted OutputOpts *OutputFormatOptions InputOpts *InputOptions // for connecting to the db SessionProvider *db.SessionProvider ExportOutput ExportOutput }
MongoExport is a container for the user-specified options and internal state used for running mongoexport.
func (*MongoExport) Export ¶
func (exp *MongoExport) Export(out io.Writer) (int64, error)
Export executes the entire export operation. It returns an integer of the count of documents successfully exported, and a non-nil error if something went wrong during the export operation.
func (*MongoExport) GetOutputWriter ¶
func (exp *MongoExport) GetOutputWriter() (io.WriteCloser, error)
GetOutputWriter opens and returns an io.WriteCloser for the output options or nil if none is set. The caller is responsible for closing it.
func (*MongoExport) ValidateSettings ¶
func (exp *MongoExport) ValidateSettings() error
ValidateSettings returns an error if any settings specified on the command line were invalid, or nil if they are valid.
type OutputFormatOptions ¶
type OutputFormatOptions struct { // Fields is an option to directly specify comma-separated fields to export to CSV. Fields string `long:"fields" short:"f" description:"comma separated list of field names (required for exporting CSV) e.g. -f \"name,age\" "` // FieldFile is a filename that refers to a list of fields to export, 1 per line. FieldFile string `long:"fieldFile" description:"file with field names - 1 per line"` // Type selects the type of output to export as (json or csv). Type string `long:"type" default:"json" default-mask:"-" description:"the output format, either json or csv (defaults to 'json')"` // OutputFile specifies an output file path. OutputFile string `long:"out" short:"o" description:"output file; if not specified, stdout is used"` // JSONArray if set will export the documents an array of JSON documents. JSONArray bool `long:"jsonArray" description:"output to a JSON array rather than one object per line"` // Pretty displays JSON data in a human-readable form. Pretty bool `long:"pretty" description:"output JSON formatted to be human-readable"` }
OutputFormatOptions defines the set of options to use in formatting exported data.
func (*OutputFormatOptions) Name ¶
func (*OutputFormatOptions) Name() string
Name returns a human-readable group name for output format options.