mgutil

package
v0.0.0-...-987d275 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MongoClient

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

MongoClient represents a MongoDB client.

func NewMongoClient

func NewMongoClient(uri string) (*MongoClient, error)

NewMongoClient creates a new instance of MongoClient by connecting to a MongoDB server and returns a pointer to the MongoClient and an error. The function takes a URI string as input, which specifies the MongoDB connection details. If the connection is successful, a new instance of MongoClient is created with the connected client, and the pointer to the MongoClient is returned along with a nil error. If the connection fails, a nil pointer is returned along with the error.

func (*MongoClient) Aggregate

func (mc *MongoClient) Aggregate(database, collection string, pipeline []bson.M) (*mongo.Cursor, error)

func (*MongoClient) Close

func (mc *MongoClient) Close() error

Close closes the connection to the MongoDB server. It calls the Disconnect method on the underlying mongo.Client object.

func (*MongoClient) CountDocuments

func (mc *MongoClient) CountDocuments(database, collection string, filter interface{}) (int64, error)

func (*MongoClient) Create

func (mc *MongoClient) Create(database, collection string, document interface{}) error

Create inserts a document into the specified database and collection. It returns an error if the insertion fails. Parameters: - database: the name of the database - collection: the name of the collection - document: the document to be inserted Returns: - error: an error indicating if the insertion was successful or not

func (*MongoClient) CreateIndex

func (mc *MongoClient) CreateIndex(database, collection string, keys interface{}, opts *options.IndexOptions) (string, error)

func (*MongoClient) Delete

func (mc *MongoClient) Delete(database, collection string, filter interface{}) error

Delete deletes a document from the specified database and collection based on the given filter. It returns an error if the deletion operation fails. The filter is used to identify the document to be deleted. Use with caution as this operation permanently removes the matching document(s).

func (*MongoClient) DeleteMany

func (mc *MongoClient) DeleteMany(database, collection string, filter interface{}) error

DeleteMany deletes multiple documents from the specified database and collection

func (*MongoClient) Distinct

func (mc *MongoClient) Distinct(database, collection, field string, filter interface{}) ([]interface{}, error)

func (*MongoClient) DropCollection

func (mc *MongoClient) DropCollection(database, collection string) error

func (*MongoClient) DropDatabase

func (mc *MongoClient) DropDatabase(database string) error

func (*MongoClient) FindAll

func (mc *MongoClient) FindAll(database string, collection string, results *[]bson.M) error

FindAll retrieves all documents from a specified collection in a database. It takes the database and collection names as parameters, as well as a pointer to a slice of bson.M, which will store the results. It returns an error if the operation fails.

func (*MongoClient) InsertMany

func (mc *MongoClient) InsertMany(database, collection string, documents []interface{}) error

InsertMany inserts multiple documents into the specified database and collection

func (*MongoClient) ListCollections

func (mc *MongoClient) ListCollections(database string) ([]string, error)

func (*MongoClient) ListDatabases

func (mc *MongoClient) ListDatabases() (mongo.ListDatabasesResult, error)

func (*MongoClient) Read

func (mc *MongoClient) Read(database, collection string, filter interface{}, result interface{}) error

Read retrieves a single document from the specified database and collection that matches the given filter and decodes the result into the provided result object. If an error occurs during the operation, it is returned. The Read operation uses the context.Background() as the context.

Parameters: - database: The name of the database. - collection: The name of the collection within the database. - filter: The filter to apply when searching for the document. - result: A pointer to the object where the result will be decoded into.

Returns: - error: An error if any occurred during the operation, otherwise it is nil.

func (*MongoClient) RunTransaction

func (mc *MongoClient) RunTransaction(database string, callback func(sessCtx mongo.SessionContext) (interface{}, error)) (interface{}, error)

RunTransaction executes the provided callback function within a MongoDB transaction

func (*MongoClient) Update

func (mc *MongoClient) Update(database, collection string, filter, update interface{}) error

Update updates a document in the specified database and collection.

Parameters: - database: The name of the database. - collection: The name of the collection. - filter: The filter to identify the document to update. - update: The update document.

Returns: - error: An error if the operation fails, or nil if successful.

func (*MongoClient) UpdateMany

func (mc *MongoClient) UpdateMany(database, collection string, filter interface{}, update interface{}) error

UpdateMany updates multiple documents in the specified database and collection

Jump to

Keyboard shortcuts

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