mongodb

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

README

go-mongodb

MongoDB client

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*driver.Client
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, url string, opts ...ClientOpt) (*Client, error)

Connect to MongoDB server

func (*Client) Close

func (client *Client) Close() error

func (*Client) Collections

func (client *Client) Collections(ctx context.Context) ([]string, error)

Collections returns the list of collections in the default database

func (*Client) Database

func (client *Client) Database(name ...string) *Database

Return a database, or default database if no argument is provided

func (*Client) Databases

func (client *Client) Databases(ctx context.Context) ([]*Database, error)

Databases returns the list of databases on the server

func (*Client) Delete

func (client *Client) Delete(ctx context.Context, collection any, filter ...*Filter) (int64, error)

Delete deletes a single document from the collection and returns the number of documents deleted, which should be zero or one. At least one filter is needed in order to select the document to delete

func (*Client) DeleteMany

func (client *Client) DeleteMany(ctx context.Context, collection any, filter ...*Filter) (int64, error)

DeleteMany deletes a zero or more documents from the collection and returns the number of documents deleted. At least one filter is needed in order to select the documents to delete

func (*Client) Do

func (client *Client) Do(ctx context.Context, fn func(context.Context) error) error

Do executes a series of statements within a transaction, and commits or rolls back depending on error returned

func (*Client) Exists

func (client *Client) Exists(ctx context.Context, name string) bool

Exists returns true if a specific database with given name exists

func (*Client) Find

func (client *Client) Find(ctx context.Context, doc any, sort *Sort, filter ...*Filter) error

Find locates a document in the collection after filtering and sorting. Returns ErrNotFound if no document was found

func (*Client) FindMany

func (client *Client) FindMany(ctx context.Context, collection any, sort *Sort, filter ...*Filter) (*Cursor, error)

FindMany returns in a collection based on filter and sort parameters, and returns an iteratable cursor to the result set. If more than one filter expression is provided, they are ANDed together

func (*Client) Insert

func (client *Client) Insert(ctx context.Context, v any) (string, error)

Insert a single document to the database and return key for the document If writable, the document InsertID field is updated

func (*Client) InsertMany

func (client *Client) InsertMany(ctx context.Context, v ...any) ([]string, error)

InsertMany inserts one or more documents of the same type to the database and return keys for the document

func (*Client) String

func (client *Client) String() string

func (*Client) Timeout

func (client *Client) Timeout() time.Duration

Timeout returns the timeout for the client

func (*Client) Update

func (client *Client) Update(ctx context.Context, update any, filter ...*Filter) (int64, int64, error)

Update a single document in a collection, and returns number of documents matched and modified

type ClientOpt

type ClientOpt func(*Client) error

func OptDatabase

func OptDatabase(v string) ClientOpt

Set the current database

type Cursor

type Cursor struct {
	*driver.Cursor
	// contains filtered or unexported fields
}

func (*Cursor) Next

func (cursor *Cursor) Next(ctx context.Context, doc any) error

type Database

type Database struct {
	*driver.Database
}

func (*Database) Collections

func (database *Database) Collections(ctx context.Context) ([]string, error)

Collections returns the list of collections in the database

func (*Database) Delete

func (database *Database) Delete(ctx context.Context, collection any, filter ...*Filter) (int64, error)

Delete a single document from collection and returns the number of deleted documents. If more than one filter expression is provided, they are ANDed together

func (*Database) DeleteMany

func (database *Database) DeleteMany(ctx context.Context, collection any, filter ...*Filter) (int64, error)

DeleteMany deletes zero or more documents from collection and returns the number of deleted documents. If more than one filter expression is provided, they are ANDed together

func (*Database) Find

func (database *Database) Find(ctx context.Context, doc any, sort *Sort, filter ...*Filter) error

Find selects a single document in a collection based on filter and sort parameters. If more than one filter expression is provided, they are ANDed together

func (*Database) FindMany

func (database *Database) FindMany(ctx context.Context, collection any, sort *Sort, filter ...*Filter) (*Cursor, error)

FindMany returns in a collection based on filter and sort parameters, and returns an iteratable cursor to the result set. If more than one filter expression is provided, they are ANDed together

func (*Database) Insert

func (database *Database) Insert(ctx context.Context, v any) (string, error)

Insert a single document to the database and return key for the document If writable, the document InsertID field is updated

func (*Database) InsertMany

func (database *Database) InsertMany(ctx context.Context, v ...any) ([]string, error)

Insert one or more documents to the database and return number of documents matched and modified all the document types must be the same

func (*Database) String

func (database *Database) String() string

func (*Database) Update

func (database *Database) Update(ctx context.Context, update any, filter ...*Filter) (int64, int64, error)

Update a single document in a collection, and returns number of documents matched and modified

type Filter

type Filter struct {
	bson.M
}

func F

func F() *Filter

func (*Filter) EqualsId

func (filter *Filter) EqualsId(id string) *Filter

EqualsId filter for a document with a specific id. It will return the same filter or nil if it was an invalid value

type Sort

type Sort struct {
	bson.D
}

func S

func S() *Sort

Jump to

Keyboard shortcuts

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