mongokit

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotMatched = errors.New("not matched")

ErrNotMatched is returned by query operators if the document does not match.

View Source
var ExpressionExtractOperators = map[string]Operator{}

ExpressionExtractOperators defines the expression extract operators.

View Source
var ExpressionQueryOperators = map[string]Operator{}

ExpressionQueryOperators defines the expression query operators.

View Source
var FieldUpdateOperators = map[string]Operator{}

FieldUpdateOperators defines the field update operators.

View Source
var ProjectionExpressionOperators = map[string]Operator{}

ProjectionExpressionOperators defines the available projection operators.

View Source
var TopLevelExtractOperators = map[string]Operator{}

TopLevelExtractOperators defines the top level extract operators.

View Source
var TopLevelQueryOperators = map[string]Operator{}

TopLevelQueryOperators defines the top level query operators

Functions

func Columns

func Columns(doc bsonkit.Doc) ([]bsonkit.Column, error)

Columns will return columns from a MongoDB sort document.

func Distinct

func Distinct(list bsonkit.List, path string) bson.A

Distinct will perform a MongoDB distinct value search on the list of documents and return an array with the results.

func Extract

func Extract(query bsonkit.Doc) (bsonkit.Doc, error)

Extract will extract the constant parts of a MongoDB query. The returned document may be used as the basis of an upsert operation.

func Filter

func Filter(list bsonkit.List, query bsonkit.Doc, limit int) (bsonkit.List, error)

Filter will filter a list of documents based on the specified MongoDB query document. A limit may be set to return early then the list is full.

func Match

func Match(doc, query bsonkit.Doc) (bool, error)

Match will test if the specified document matches the supplied MongoDB query document.

func Process

func Process(ctx Context, doc bsonkit.Doc, query bson.D, prefix string, root bool) error

Process will process a document with a query using the MongoDB operator processing algorithm.

func ProcessExpression

func ProcessExpression(ctx Context, doc bsonkit.Doc, prefix string, pair bson.E, root bool) error

ProcessExpression will process a document with a query using the MongoDB operator algorithm.

func Project

func Project(doc, projection bsonkit.Doc) (bsonkit.Doc, error)

Project will apply the specified project to the document and return the resulting document.

func ProjectList

func ProjectList(list bsonkit.List, projection bsonkit.Doc) (bsonkit.List, error)

ProjectList will apply the provided projection to the specified list.

func Resolve added in v0.2.0

func Resolve(path string, query, doc bsonkit.Doc, arrayFilters bsonkit.List, callback func(path string) error) error

Resolve will resolve all positional operators in the provided path using the query, document and array filters. For each match it will call the callback with the generated absolute path.

func Sort

func Sort(list bsonkit.List, doc bsonkit.Doc) (bsonkit.List, error)

Sort will sort a list based on a MongoDB sort document and return a new list with sorted documents.

func SplitDynamicPath added in v0.2.0

func SplitDynamicPath(path string) (string, string, string)

SplitDynamicPath will split the provided path on the first positional operator. It will return the leading path, the operator and the trailing path. The segments may be set to bsonkit.PathEnd if there are not available in the path.

Types

type Changes

type Changes struct {
	// Whether the operation was an upsert.
	Upsert bool

	// The fields that have been added, updated or removed in the document.
	// Added and updated fields are set to the final value while removed fields
	// are set to bsonkit.Missing.
	Changed map[string]interface{}
	// contains filtered or unexported fields
}

Changes record the applied changes to a document.

func Apply

func Apply(doc, query, update bsonkit.Doc, upsert bool, arrayFilters bsonkit.List) (*Changes, error)

Apply will apply a MongoDB update document on a document using the various update operators. The document is updated in place. The changes to the document are recorded and returned.

func Update

func Update(list bsonkit.List, query, update bsonkit.Doc, upsert bool, arrayFilters bsonkit.List) ([]*Changes, error)

Update will apply a MongoDB update document to a list of documents.

func (*Changes) Record added in v0.2.3

func (c *Changes) Record(path string, val interface{}) error

Record will record a field change. If the value is bsonkit.Missing it will record a removal. It will return an error if a path is conflicting with a previous recorded change.

type Collection

type Collection struct {
	Documents *bsonkit.Set
	Indexes   map[string]*Index
}

Collection combines a set and multiple indexes to form a basic MongoDB like collection that offers basic CRUD capabilities. The collection is not safe from concurrent access and does not roll back changes on errors. Therefore, the recommended approach is to clone the collection before making changes.

func NewCollection

func NewCollection(idIndex bool) *Collection

NewCollection will create and return a new collection.

func (*Collection) Clone

func (c *Collection) Clone() *Collection

Clone will clone the collection.

func (*Collection) CreateIndex

func (c *Collection) CreateIndex(name string, config IndexConfig) (string, error)

CreateIndex will create and build an index based on the specified configuration. If the index name is missing, it will be generated from the config and returned.

func (*Collection) Delete

func (c *Collection) Delete(query, sort bsonkit.Doc, skip, limit int) (*Result, error)

Delete will remove all documents that match the specified query.

func (*Collection) DropIndex

func (c *Collection) DropIndex(name string) ([]string, error)

DropIndex will drop the specific index or drop all indexes if no name has been specified.

func (*Collection) Find

func (c *Collection) Find(query, sort bsonkit.Doc, skip, limit int) (*Result, error)

Find will look up the documents that match the specified query.

func (*Collection) Insert

func (c *Collection) Insert(doc bsonkit.Doc) (*Result, error)

Insert will add the specified document to the collection.

func (*Collection) Replace

func (c *Collection) Replace(query, repl, sort bsonkit.Doc) (*Result, error)

Replace will look up the first document that matches the query and if found replace it with the specified document.

func (*Collection) Update

func (c *Collection) Update(query, update, sort bsonkit.Doc, skip, limit int, arrayFilters bsonkit.List) (*Result, error)

Update will look up all documents that match the specified query and update them according to the update document.

func (*Collection) Upsert

func (c *Collection) Upsert(query, repl, update bsonkit.Doc, arrayFilters bsonkit.List) (*Result, error)

Upsert will insert a document based on the specified query and either the replacement document or update document.

type Context

type Context struct {
	// The available top level operators.
	TopLevel map[string]Operator

	// The available expression operators.
	Expression map[string]Operator

	// Whether missing operators should just be skipped.
	SkipMissing bool

	// If enabled, top level operators will expect a document with multiple
	// invocations of the operator.
	MultiTopLevel bool

	// A custom value available to the operators.
	Value interface{}

	// The query used to resolve positional operators in top level operator
	// invocation paths.
	TopLevelQuery bsonkit.Doc

	// The array filters used to resolve positional operators in top level
	// operator invocation paths.
	TopLevelArrayFilters bsonkit.List
}

Context is the context passed to operators.

type Index

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

Index is an index for documents that supports MongoDB features. The index is not safe from concurrent access and does not roll back changes on errors. Therefore, the recommended approach is to clone the index before making changes.

func CreateIndex

func CreateIndex(config IndexConfig) (*Index, error)

CreateIndex will create and return a new index.

func (*Index) Add

func (i *Index) Add(doc bsonkit.Doc) (bool, error)

Add will add the document to index. May return false if the document has already been added to the index. If the document has been skipped due to a partial filter true is returned.

func (*Index) Build

func (i *Index) Build(list bsonkit.List) (bool, error)

Build will build the index from the specified list. It may return false if there was a unique constraint error when building the index.

func (*Index) Clone

func (i *Index) Clone() *Index

Clone will clone the index. Mutating the new index will not mutate the original index.

func (*Index) Config

func (i *Index) Config() IndexConfig

Config will return the index configuration.

func (*Index) Has

func (i *Index) Has(doc bsonkit.Doc) (bool, error)

Has returns whether the specified document has been added to the index.

func (*Index) List added in v0.1.5

func (i *Index) List() bsonkit.List

List will return an ascending list of all documents in the index.

func (*Index) Remove

func (i *Index) Remove(doc bsonkit.Doc) (bool, error)

Remove will remove a document from the index. May return false if the document has not yet been added to the index.

type IndexConfig

type IndexConfig struct {
	// The index key.
	Key bsonkit.Doc

	// Whether the index is unique.
	Unique bool

	// The partial index filter.
	Partial bsonkit.Doc

	// The time after documents expire.
	Expiry time.Duration
}

IndexConfig defines an index configuration.

func (IndexConfig) Equal added in v0.1.2

func (c IndexConfig) Equal(d IndexConfig) bool

Equal will compare to configurations and return whether they are equal.

func (IndexConfig) Name added in v0.1.2

func (c IndexConfig) Name() (string, error)

Name will return the computed index name.

type Operator

type Operator func(ctx Context, doc bsonkit.Doc, op, path string, v interface{}) error

Operator is a generic operator.

type Result

type Result struct {
	// The list of found or deleted documents.
	Matched bsonkit.List

	// The list of inserted, replaced or updated documents.
	Modified bsonkit.List

	// The upserted document.
	Upserted bsonkit.Doc

	// The changes applied to updated documents.
	Changes []*Changes
}

Result is returned by collection operations.

Jump to

Keyboard shortcuts

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