mongo-tools: github.com/mongodb/mongo-tools/mongoexport Index | Files | Directories

package mongoexport

import "github.com/mongodb/mongo-tools/mongoexport"

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

Index

Package Files

csv.go json.go mongoexport.go options.go

Constants

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

Output types supported by mongoexport.

Variables

var Usage = "" /* 149 byte string literal not displayed */

type CSVExportOutput Uses

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 Uses

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 Uses

func (csvExporter *CSVExportOutput) ExportDocument(document bson.D) error

ExportDocument writes a line to output with the CSV representation of a document.

func (*CSVExportOutput) Flush Uses

func (csvExporter *CSVExportOutput) Flush() error

Flush writes any pending data to the underlying I/O stream.

func (*CSVExportOutput) WriteFooter Uses

func (csvExporter *CSVExportOutput) WriteFooter() error

WriteFooter is a no-op for CSV export formats.

func (*CSVExportOutput) WriteHeader Uses

func (csvExporter *CSVExportOutput) WriteHeader() error

WriteHeader writes a comma-delimited list of fields as the output header row.

type ExportOutput Uses

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 Uses

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:"false" default-mask:"-"`
    ReadPreference string `long:"readPreference" value-name:"<string>|<json>" description:"specify either a preference mode (e.g. 'nearest') or a preference json object (e.g. '{mode: \"nearest\", tagSets: [{a: \"b\"}], maxStalenessSeconds: 123}')"`
    ForceTableScan bool   `long:"forceTableScan" description:"force a table scan (do not use $snapshot)"`
    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" default:"false" 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 Uses

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

func (*InputOptions) HasQuery Uses

func (inputOptions *InputOptions) HasQuery() bool

func (*InputOptions) Name Uses

func (*InputOptions) Name() string

Name returns a human-readable group name for input options.

type JSONExportOutput Uses

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 Uses

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 Uses

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 Uses

func (jsonExporter *JSONExportOutput) Flush() error

Flush is a no-op for JSON export formats.

func (*JSONExportOutput) WriteFooter Uses

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 Uses

func (jsonExporter *JSONExportOutput) WriteHeader() error

WriteHeader writes the opening square bracket if in array mode, otherwise it behaves as a no-op.

type JSONFormat Uses

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 Uses

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 Uses

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

New constructs a new MongoExport instance from the provided options.

func (*MongoExport) Close Uses

func (exp *MongoExport) Close()

Close cleans up all the resources for a MongoExport instance.

func (*MongoExport) Export Uses

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 Uses

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 Uses

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

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

func ParseOptions Uses

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 Uses

type OutputFormatOptions struct {
    // Fields is an option to directly specify comma-separated fields to export to CSV.
    Fields string `long:"fields" value-name:"<field>[,<field>]*" 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" 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" default:"false" 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 `long:"jsonFormat" value-name:"<type>" default:"relaxed" description:"the extended JSON format to output, either canonical or relaxed (defaults to 'relaxed')"`
}

OutputFormatOptions defines the set of options to use in formatting exported data.

func (*OutputFormatOptions) Name Uses

func (*OutputFormatOptions) Name() string

Name returns a human-readable group name for output format options.

Directories

PathSynopsis
mainMain package for the mongoexport tool.

Package mongoexport imports 22 packages (graph) and is imported by 12 packages. Updated 2019-08-24. Refresh now. Tools for package owners.