mongoexport

package
v0.0.0-...-462fb9d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2016 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package mongoexport produces a JSON or CSV export of data stored in a MongoDB instance.

Index

Constants

View Source
const (
	CSV  = "csv"
	JSON = "json"
)

Output types supported by mongoexport.

Variables

View Source
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

	// NoHeaderLine, if set, will export CSV data without a list of field names at the first line
	NoHeaderLine bool
	// 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, noHeaderLine bool, 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.D) 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.D) error

	// WriteFooter outputs any post-record headers that are written once per
	// 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" value-name:"<json>" short:"q" description:"query filter, as a JSON string, e.g., '{x:{$gt:1}}'"`
	QueryFile      string `long:"queryFile" value-name:"<filename>" description:"path to a file containing a query filter (JSON)"`
	SlaveOk        bool   `long:"slaveOk" short:"k" description:"allow secondary reads if available (default true)" default:"false" default-mask:"-"`
	ReadPreference string `long:"readPreference" value-name:"<string>|<json>" description:"specify either a preference name or a preference json object"`
	ForceTableScan bool   `long:"forceTableScan" description:"force a table scan (do not use $snapshot)"`
	Skip           int    `long:"skip" value-name:"<count>" description:"number of documents to skip"`
	Limit          int    `long:"limit" value-name:"<count>" description:"limit the number of documents to export"`
	Sort           string `long:"sort" value-name:"<json>" 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) GetQuery

func (inputOptions *InputOptions) GetQuery() ([]byte, error)

func (*InputOptions) HasQuery

func (inputOptions *InputOptions) HasQuery() bool

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.D) 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 `` /* 156-byte string literal not displayed */

	// FieldFile is a filename that refers to a list of fields to export, 1 per line.
	FieldFile string `long:"fieldFile" value-name:"<filename>" description:"file with field names - 1 per line"`

	// Type selects the type of output to export as (json or csv).
	Type string `` /* 136-byte string literal not displayed */

	// OutputFile specifies an output file path.
	OutputFile string `long:"out" value-name:"<filename>" 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"`

	// NoHeaderLine, if set, will export CSV data without a list of field names at the first line.
	NoHeaderLine bool `long:"noHeaderLine" description:"export CSV data without a list of field names at the first line"`
}

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.

Directories

Path Synopsis
Main package for the mongoexport tool.
Main package for the mongoexport tool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL