mongoexport

package
v0.0.0-...-95a44f4 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: Apache-2.0 Imports: 22 Imported by: 20

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 = `` /* 223-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 (_ *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" hidden:"true" description:"allow secondary reads if available" default-mask:"-"`
	ReadPreference string `` /* 220-byte string literal not displayed */
	ForceTableScan bool   `` /* 146-byte string literal not displayed */
	Skip           int64  `long:"skip" value-name:"<count>" description:"number of documents to skip"`
	Limit          int64  `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}'"`
	AssertExists   bool   `long:"assertExists" description:"if specified, export fails if the collection does not exist"`
}

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
	Out          io.Writer
	NumExported  int64
	JSONFormat   JSONFormat
}

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,
	jsonFormat JSONFormat,
) *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 JSONFormat

type JSONFormat string

JSONFormat is the type for all valid extended JSON formats to output.

const (
	// Canonical indicates canonical json format.
	Canonical JSONFormat = "canonical"
	// Relaxed indicates relaxed json format.
	Relaxed JSONFormat = "relaxed"
)

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

	ProgressManager progress.Manager
	// contains filtered or unexported fields
}

MongoExport is a container for the user-specified options and internal state used for running mongoexport.

func New

func New(opts Options) (*MongoExport, error)

New constructs a new MongoExport instance from the provided options.

func (*MongoExport) Close

func (exp *MongoExport) Close()

Close cleans up all the resources for a MongoExport instance.

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.

type Options

type Options struct {
	*options.ToolOptions
	*OutputFormatOptions
	*InputOptions
	ParsedArgs []string
}

Options represents all possible options that can be used to configure mongoexport.

func ParseOptions

func ParseOptions(rawArgs []string, versionStr, gitCommit string) (Options, error)

ParseOptions reads command line arguments and converts them into options that can be used to configure mongoexport.

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 `long:"type" value-name:"<type>" default:"json" default-mask:"-" description:"the output format, either json or csv"`

	// Deprecated: allow legacy --csv option in place of --type=csv
	CSVOutputType bool `long:"csv" hidden:"true"`

	// 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"`

	// JSONFormat specifies what extended JSON format to export (canonical or relaxed). Defaults to relaxed.
	JSONFormat JSONFormat `` /* 157-byte string literal not displayed */
}

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