index

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2020 License: MIT Imports: 13 Imported by: 1

README

MongoDB Index utilities

GoDoc

This package provides a simple way to create MongoDB indexes from a YAML configuration file.

Read the Documentation and the example configuration file.

Install

go get github.com/bounoable/mongoutil

Documentation

Overview

Package index provides functions to create MongoDB indexes from YAML configuration files.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config map[string][]mongo.IndexModel

Config is the index configuration for the collections.

func ParseYAML

func ParseYAML(b []byte) (Config, error)

ParseYAML parses index models from YAML.

type Result

type Result struct {
	// How many indexes were created.
	Count int
	// Names of the created indexes categorized by collection name.
	Names map[string][]string
}

Result contains information about the created indexes.

func Create

func Create(ctx context.Context, db *mongo.Database, filepath string, opts ...*options.CreateIndexesOptions) (Result, error)

Create creates the indexes for db from the provided YAML configuration file.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/bounoable/mongoutil/index"
	"go.mongodb.org/mongo-driver/mongo"
)

func main() {
	ctx := context.Background()

	client, _ := mongo.Connect(ctx)
	db := client.Database("dbname")

	result, err := index.Create(ctx, db, "/path/to/config.yml")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("created %d indexes: \n%v\n", result.Count, result.Names)
}
Output:

func CreateFromBytes

func CreateFromBytes(ctx context.Context, db *mongo.Database, yaml []byte, opts ...*options.CreateIndexesOptions) (Result, error)

CreateFromBytes creates the indexes for db from the provided configuration in YAML format.

func CreateFromConfig

func CreateFromConfig(ctx context.Context, db *mongo.Database, cfg Config, opts ...*options.CreateIndexesOptions) (Result, error)

CreateFromConfig creates the indexes for db from the provided configuration.

func CreateFromReader

func CreateFromReader(ctx context.Context, db *mongo.Database, r io.Reader, opts ...*options.CreateIndexesOptions) (Result, error)

CreateFromReader creates the indexes for db from the provided configuration in YAML format.

func CreateFromString

func CreateFromString(ctx context.Context, db *mongo.Database, yaml string, opts ...*options.CreateIndexesOptions) (Result, error)

CreateFromString creates the indexes for db from the provided configuration in YAML format.

Jump to

Keyboard shortcuts

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