db

package
v0.0.0-...-5f5e9cb Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 16 Imported by: 188

Documentation

Overview

Package db manages Evergreen's interaction with the database.

Index

Constants

View Source
const FacetPipelineStageTooLargeCode = 4031700

FacetPipelineStageTooLargeCode is the error code for when a facet pipeline stage is too large. https://github.com/mongodb/mongo/blob/a1732172ed5d66d98582ea1059c0ede9d8cd5065/src/mongo/db/pipeline/document_source_facet.cpp#L165

Variables

View Source
var (
	NoProjection     = bson.M{}
	NoSort           = []string{}
	NoSkip           = 0
	NoLimit          = 0
	NoHint       any = nil
)

Functions

func Aggregate

func Aggregate(ctx context.Context, collection string, pipeline any, out any) error

Aggregate runs an aggregation pipeline on a collection and unmarshals the results to the given "out" interface (usually a pointer to an array of structs/bson.M)

func Clear

func Clear(collection string) error

Clear removes all documents from a specified collection.

func ClearCollections

func ClearCollections(collections ...string) error

ClearCollections clears all documents from all the specified collections, returning an error immediately if clearing any one of them fails.

func ClearGridCollections

func ClearGridCollections(fsPrefix string) error

func Count

func Count(ctx context.Context, collection string, query any) (int, error)

Count run a count command with the specified query against the collection.

func CountQ

func CountQ(ctx context.Context, collection string, q Q) (int, error)

CountQ runs a Q count query against the given collection.

func CreateCollections

func CreateCollections(collections ...string) error

CreateCollections ensures that all the given collections are created, returning an error immediately if creating any one of them fails.

func DropCollections

func DropCollections(collections ...string) error

DropCollections drops the specified collections, returning an error immediately if dropping any one of them fails.

func EnsureIndex

func EnsureIndex(collection string, index mongo.IndexModel) error

EnsureIndex takes in a collection and ensures that the index is created if it does not already exist.

func FindAllQ

func FindAllQ(ctx context.Context, collection string, q Q, out any) error

FindAllQ runs a Q query against the given collection, applying the results to "out."

func FindAndModify

func FindAndModify(ctx context.Context, collection string, query any, sort []string, change db.Change, out any) (*db.ChangeInfo, error)

FindAndModify runs the specified query and change against the collection, unmarshaling the result into the specified interface.

func FindOneQ

func FindOneQ(ctx context.Context, collection string, q Q, out any) error

FindOneQ runs a Q query against the given collection, applying the results to "out." Only reads one document from the DB.

func GetGridFile

func GetGridFile(ctx context.Context, fsPrefix, name string) (io.ReadCloser, error)

GetGridFile returns a ReadCloser for a file stored with the given name under the GridFS prefix.

func Insert

func Insert(ctx context.Context, collection string, item any) error

Insert inserts the specified item into the specified collection.

func InsertMany

func InsertMany(ctx context.Context, collection string, items ...any) error

func InsertManyUnordered

func InsertManyUnordered(ctx context.Context, collection string, items ...any) error

func InsertWithEnv

func InsertWithEnv(ctx context.Context, env evergreen.Environment, collection string, item any) error

InsertWithEnv inserts the specified item into the specified collection using the given environment's database. This is useful for transactions where the same client must be used for all operations.

func IsDocumentLimit

func IsDocumentLimit(err error) bool

func IsDuplicateKey

func IsDuplicateKey(err error) bool

func IsErrorCode

func IsErrorCode(err error, errorCode int) bool

IsErrorCode checks if the error is a mongo error with the given error code.

func Remove

func Remove(ctx context.Context, collection string, query any) error

Remove removes one item matching the query from the specified collection.

func RemoveAll

func RemoveAll(ctx context.Context, collection string, query any) error

RemoveAll removes all items matching the query from the specified collection.

func RemoveAllQ

func RemoveAllQ(ctx context.Context, collection string, q Q) error

RemoveAllQ removes all docs that satisfy the query

func Replace

func Replace(ctx context.Context, collection string, query any, replacement any) (*db.ChangeInfo, error)

Replace replaces one matching document in the collection. If a matching document is not found, it will be upserted. It returns the upserted ID if one was created.

func Update

func Update(ctx context.Context, collection string, query any, update any) error

Update updates one matching document in the collection.

func UpdateAll

func UpdateAll(ctx context.Context, collection string, query any, update any) (*db.ChangeInfo, error)

UpdateAll updates all matching documents in the collection.

func UpdateId

func UpdateId(ctx context.Context, collection string, id, update any) error

UpdateId updates one _id-matching document in the collection.

func Upsert

func Upsert(ctx context.Context, collection string, query any, update any) (*db.ChangeInfo, error)

Upsert run the specified update against the collection as an upsert operation.

func WriteGridFile

func WriteGridFile(ctx context.Context, fsPrefix, name string, source io.Reader) error

WriteGridFile writes the data in the source Reader to a GridFS collection with the given prefix and filename.

Types

type Q

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

Q holds all information necessary to execute a query

func Query

func Query(filter any) Q

Query creates a db.Q for the given MongoDB query. The filter can be a struct, bson.D, bson.M, nil, etc.

func (Q) Filter

func (q Q) Filter(filter any) Q

func (Q) GetBSON

func (q Q) GetBSON() (any, error)

func (Q) Hint

func (q Q) Hint(hint any) Q

Hint sets the hint for a query to determine what index will be used. The hint can be either the index as an ordered document of the keys or a

func (Q) Limit

func (q Q) Limit(limit int) Q

func (Q) MarshalBSON

func (q Q) MarshalBSON() ([]byte, error)

func (Q) MaxTime

func (q Q) MaxTime(maxTime time.Duration) Q

MaxTime sets the maxTime for a query to time out the query on the db server.

func (Q) Project

func (q Q) Project(projection any) Q

func (Q) SetBSON

func (q Q) SetBSON(_ bson.Raw) error

func (Q) Skip

func (q Q) Skip(skip int) Q

func (Q) Sort

func (q Q) Sort(sort []string) Q

func (Q) UnmarshalBSON

func (q Q) UnmarshalBSON(_ []byte) error

func (Q) WithFields

func (q Q) WithFields(fields ...string) Q

func (Q) WithoutFields

func (q Q) WithoutFields(fields ...string) Q

type SessionFactory

type SessionFactory interface {
	// GetSession uses the provided context to get a session and database.
	GetSession(ctx context.Context) (db.Session, db.Database, error)
}

func GetCedarGlobalSessionFactory

func GetCedarGlobalSessionFactory() SessionFactory

GetCedarGlobalSessionFactory initializes a session factory to connect to the Cedar database.

func GetGlobalSessionFactory

func GetGlobalSessionFactory() SessionFactory

GetGlobalSessionFactory initializes a session factory to connect to the Evergreen database.

Directories

Path Synopsis
mgo
Package mgo provides support for Evergreen's remaining usages of the legacy MongoDB driver gopkg.in/mgo.v2.
Package mgo provides support for Evergreen's remaining usages of the legacy MongoDB driver gopkg.in/mgo.v2.
bson
Package bson is an implementation of the BSON specification for Go:
Package bson is an implementation of the BSON specification for Go:
internal/json
Package json implements encoding and decoding of JSON as defined in RFC 4627.
Package json implements encoding and decoding of JSON as defined in RFC 4627.

Jump to

Keyboard shortcuts

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