mongocontainer

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: MIT Imports: 4 Imported by: 0

README

Mongo Container

mongocontainer is a Go package that provides a simple interface for interacting with MongoDB. It provides a MongoImpl interface that defines the basic MongoDB operations, such as Find, Upsert, and Delete, as well as a Disconnect method to gracefully disconnect from the database.

The package also provides a Setup function that takes a MongoDB connection URI as an argument and returns a MongoImpl object for use in your application. This makes it easy to set up a MongoDB connection in your Go application without having to worry about the low-level details of managing the connection.

Installation

To use mongocontainer in your Go project, you can simply import it as follows:

import "github.com/6d6577/mongocontainer"

You can then use the Setup function to establish a MongoDB connection:

mongoImpl, err := mongocontainer.Setup("mongodb://localhost:27017")
if err != nil {
    log.Fatal(err)
}
defer mongoImpl.Disconnect()

Usage

Here's an example of how you can use mongocontainer to perform a Find operation on a MongoDB collection:

result := mongoImpl.Find("users", bson.M{"name": "John Doe"})
var user User
err := result.Decode(&user)
if err != nil {
    log.Fatal(err)
}

In this example, we're finding a user with the name "John Doe" in the users collection. We then decode the result into a User struct using the Decode method provided by the MongoDB driver.

Contributing

Contributions to mongocontainer are welcome and encouraged! If you find a bug or would like to suggest a new feature, please open an issue on the GitHub repository.

License

This package is licensed under the MIT License. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mongo

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

Mongo contains the mongo client and database instances

func (*Mongo) Disconnect

func (m *Mongo) Disconnect() error

Disconnect disconnects the mongo client

func (*Mongo) FindOne

func (m *Mongo) FindOne(collection string, filter interface{}) *mongo.SingleResult

FindOne finds a single document that matches the filter in the given collection

func (*Mongo) FindOneAndDelete

func (m *Mongo) FindOneAndDelete(collection string, filter interface{}) *mongo.SingleResult

FindOneAndDelete deletes a document that matches the filter in the given collection

func (*Mongo) Upsert

func (m *Mongo) Upsert(collection string, filter interface{}, update interface{}) *mongo.SingleResult

Upsert updates a document if it already exists or inserts it otherwise

type MongoImpl

type MongoImpl interface {
	FindOne(collection string, filter interface{}) *mongo.SingleResult
	Upsert(collection string, filter interface{}, update interface{}) *mongo.SingleResult
	FindOneAndDelete(collection string, filter interface{}) *mongo.SingleResult
	Disconnect() error
}

MongoImpl defines the interface for interacting with MongoDB

func Setup

func Setup(uri string, database string) (MongoImpl, error)

Setup connects to the MongoDB database and returns a Mongo instance

Jump to

Keyboard shortcuts

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