mongodb

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README


title: 'MongoDB'

MongoDB connector allows querying MongoDB databases using their native query language.

Config Schema

Field Type Required Description
type string yes constant: mongodb
hosts string[] yes List of MongoDB hosts (e.g., ["localhost:27017"])
database string yes Database name
username string yes Username for authentication
password string yes Password for authentication
is_readonly boolean no Whether to open database in read-only mode
conn_string string no Direct connection string

Config example:

connection:
    type: mongodb
    hosts:
    - localhost:27017
    - mongodb-node2:27017
    database: mydb
    username: admin
    password: secret
    is_readonly: false

Or as alternative with direct connection string:

connection:
    type: mongodb
    conn_string: mongodb://admin:secret@localhost:27017/mydb

Query Format

MongoDB queries must be written in JSON format with the following structure:

{
    "collection": "users",
    "filter": {
        "age": {"$gt": "@minAge"},
        "status": "@status"
    }
}

Query Parameters

  • Use @paramName syntax for parameter substitution
  • Parameters are automatically replaced with their values
  • Supports all MongoDB query operators ($gt, $lt, $in, etc.)

Pagination

For pagination, use MongoDB's native operators:

  • skip: Number of documents to skip
  • limit: Maximum number of documents to return

Example with pagination:

{
    "collection": "users",
    "filter": {
        "status": "@status"
    },
    "skip": "@offset",
    "limit": "@limit"
}

Notes

  • The connector uses the official MongoDB Go driver
  • For high availability, specify multiple hosts - the client will automatically handle failover
  • Authentication is required for all connections
  • The database parameter is required and specifies the default database for queries
  • Supports MongoDB version 4.0 and above
  • Queries are executed using MongoDB's native query language
  • Schema discovery is based on sample documents from collections

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Hosts      []string `yaml:"hosts"`
	Database   string   `yaml:"database"`
	Username   string   `yaml:"username"`
	Password   string   `yaml:"password"`
	IsReadonly bool     `yaml:"is_readonly"`
	ConnString string   `yaml:"conn_string"`
}

func (Config) ConnectionString

func (c Config) ConnectionString() string

func (Config) Doc

func (c Config) Doc() string

func (Config) ExtraPrompt

func (c Config) ExtraPrompt() []string

func (Config) Readonly

func (c Config) Readonly() bool

func (Config) Type

func (c Config) Type() string

func (*Config) UnmarshalYAML

func (c *Config) UnmarshalYAML(value *yaml.Node) error

func (Config) Validate

func (c Config) Validate() error

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector implements the connectors.Connector interface for MongoDB

func (Connector) Config

func (c Connector) Config() connectors.Config

func (*Connector) Discovery

func (c *Connector) Discovery(ctx context.Context, tablesList []string) ([]model.Table, error)

func (*Connector) GuessColumnType

func (c *Connector) GuessColumnType(mongoType string) model.ColumnType

func (*Connector) InferQuery

func (c *Connector) InferQuery(ctx context.Context, query string) ([]model.ColumnSchema, error)

func (Connector) Ping

func (c Connector) Ping(ctx context.Context) error

Ping checks if MongoDB is reachable

func (*Connector) Query

func (c *Connector) Query(ctx context.Context, endpoint model.Endpoint, params map[string]any) ([]map[string]any, error)

func (*Connector) Sample

func (c *Connector) Sample(ctx context.Context, table model.Table) ([]map[string]any, error)

Jump to

Keyboard shortcuts

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