Documentation ¶
Overview ¶
Package mongo is a simple wrapper for MongoDb Driver, this package uses "_id" instead of "_id" to find or add a document.
It is important to know that you will have to index id field for optimum performance.
In general, you wouldn't need this package at all, if you rely more on "_id" and simple access to MongoDB API than this module will help you.
Example:
import "github.com/akshaybabloo/mongo" type data struct { ID int `bson:"_id"` Name string `bson:"name"` } func main() { client := NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test") testData := data{ ID: 1, Name: "Akshay", } done, err := client.Add("test_collection", testData) if err != nil { panic(err) } print(done.InsertedID) }
Index ¶
- type Client
- func (connectionDetails *Client) Add(collectionName string, data interface{}) (*mongo.InsertOneResult, error)
- func (connectionDetails *Client) AddMany(collectionName string, data []interface{}) (*mongo.InsertManyResult, error)
- func (connectionDetails *Client) Collection(collectionName string) (*mongo.Collection, *mongo.Client, context.Context, error)
- func (connectionDetails *Client) DB() (*mongo.Database, error)
- func (connectionDetails *Client) Delete(collectionName string, id string) (*mongo.DeleteResult, error)
- func (connectionDetails *Client) DeleteCustom(collectionName string, filter interface{}) (*mongo.DeleteResult, error)
- func (connectionDetails *Client) DeleteMany(collectionName string, filter interface{}) (*mongo.DeleteResult, error)
- func (connectionDetails *Client) Get(collectionName string, id string) (*mongo.SingleResult, error)
- func (connectionDetails *Client) GetAll(collectionName string, id string, result interface{}) error
- func (connectionDetails *Client) GetAllCustom(collectionName string, filter interface{}, result interface{}) error
- func (connectionDetails *Client) GetCustom(collectionName string, filter interface{}) (*mongo.SingleResult, error)
- func (connectionDetails *Client) RawClient() (*mongo.Client, error)
- func (connectionDetails *Client) Update(collectionName string, id string, data interface{}) (*mongo.UpdateResult, error)
- func (connectionDetails *Client) UpdateCustom(collectionName string, filter interface{}, data interface{}, ...) (*mongo.UpdateResult, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // ConnectionUrl which connects to MongoDB atlas or local deployment ConnectionUrl string // DatabaseName with database name DatabaseName string // Highly recommend using timeout Context Context context.Context }
Client takes in the
func NewMongoClient ¶
NewMongoClient returns Client and it's associated functions
func NewMongoClientDefault ¶
NewMongoClientDefault returns Client, and it's associated functions with default context
func (*Client) Add ¶
func (connectionDetails *Client) Add(collectionName string, data interface{}) (*mongo.InsertOneResult, error)
Add can be used to add document to MongoDB
Example ¶
type data struct { ID string `bson:"_id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) testData := data{ ID: "1", Name: "Akshay", } done, err := client.Add("test_collection", testData) if err != nil { panic(err) } fmt.Println("The ID is:", done.InsertedID)
Output:
func (*Client) AddMany ¶
func (connectionDetails *Client) AddMany(collectionName string, data []interface{}) (*mongo.InsertManyResult, error)
AddMany can be used to add multiple documents to MongoDB
Example ¶
type data struct { ID string `bson:"_id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) var testData = []interface{}{ data{ ID: "1", Name: "Akshay", }, data{ ID: "2", Name: "Raj", }, } done, err := client.AddMany("test_collection", testData) if err != nil { panic(err) } fmt.Println("The ID is:", done.InsertedIDs)
Output:
func (*Client) Collection ¶
func (connectionDetails *Client) Collection(collectionName string) (*mongo.Collection, *mongo.Client, context.Context, error)
Collection returns mongo.Collection
Note: Do not forget to do - defer Client.Disconnect(ctx)
func (*Client) Delete ¶
func (connectionDetails *Client) Delete(collectionName string, id string) (*mongo.DeleteResult, error)
Delete deletes a document by ID only.
Example ¶
client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) deleted, err := client.Delete("test_collection", "1") if err != nil { panic(err) } fmt.Println("Deleted items:", deleted.DeletedCount)
Output:
func (*Client) DeleteCustom ¶ added in v4.1.0
func (connectionDetails *Client) DeleteCustom(collectionName string, filter interface{}) (*mongo.DeleteResult, error)
DeleteCustom deletes a document by a filter - bson.M{}, bson.A{}, or bson.D{}
func (*Client) DeleteMany ¶ added in v4.1.0
func (connectionDetails *Client) DeleteMany(collectionName string, filter interface{}) (*mongo.DeleteResult, error)
DeleteMany deletes many documents - bson.M{}, bson.A{}, or bson.D{}
Example ¶
type data struct { ID string `bson:"_id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) deleted, err := client.DeleteMany("test_collection", bson.M{"_id": bson.M{"$in": bson.A{"1", "2"}}}) if err != nil { panic(err) } fmt.Println("Deleted items:", deleted.DeletedCount)
Output:
func (*Client) Get ¶
Get finds one document based on "_id"
Example ¶
type data struct { ID int `bson:"_id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) var decodeData data get, err := client.Get("test_collection", "2") if err != nil { panic("Something went wrong") } err = get.Decode(&decodeData) if err != nil { panic("No data found.") } fmt.Println(decodeData)
Output:
func (*Client) GetAll ¶
GetAll finds all documents by "_id".
The 'result' parameter needs to be a pointer.
Example ¶
type data struct { ID string `bson:"_id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) var testData []data err := client.GetAll("test_collection", "1", &data{}) if err != nil { panic(err) } fmt.Println("The ID is:", testData)
Output:
func (*Client) GetAllCustom ¶
func (connectionDetails *Client) GetAllCustom(collectionName string, filter interface{}, result interface{}) error
GetAllCustom finds all documents by filter - bson.M{}, bson.A{}, or bson.D{}.
The 'result' parameter needs to be a pointer.
Example ¶
type data struct { ID string `bson:"_id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) var testData []data err := client.GetAllCustom("test_collection", bson.M{"_id": "1"}, &data{}) if err != nil { panic(err) } fmt.Println("The ID is:", testData)
Output:
func (*Client) GetCustom ¶
func (connectionDetails *Client) GetCustom(collectionName string, filter interface{}) (*mongo.SingleResult, error)
GetCustom finds one document by a filter - bson.M{}, bson.A{}, or bson.D{}
Example ¶
type data struct { ID int `bson:"_id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) var decodeData data getCustom, err := client.GetCustom("test_collection", bson.M{"_id": "2"}) if err != nil { panic("No data found.") } err = getCustom.Decode(&decodeData) if err != nil { panic("Something went wrong") } fmt.Println(decodeData)
Output:
func (*Client) Update ¶
func (connectionDetails *Client) Update(collectionName string, id string, data interface{}) (*mongo.UpdateResult, error)
Update can be used to update values by its ID
Example ¶
type data struct { Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) testData := data{ Name: "Akshay", } updated, err := client.Update("test_collection", "1", testData) if err != nil { panic(err) } fmt.Println("Modified items:", updated.ModifiedCount)
Output:
func (*Client) UpdateCustom ¶ added in v4.1.2
func (connectionDetails *Client) UpdateCustom(collectionName string, filter interface{}, data interface{}, updateOptions ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateCustom can be used to update values by a filter - bson.M{}, bson.A{}, or bson.D{}
Example ¶
type data struct { Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test", context.Background()) testData := data{ Name: "Akshay", } updated, err := client.UpdateCustom("test_collection", bson.M{"_id": "1"}, testData) if err != nil { panic(err) } fmt.Println("Modified items:", updated.ModifiedCount)
Output: