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) 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, context.Context, error)
- func (connectionDetails *Client) Update(collectionName string, id string, 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 }
Client takes in the
func NewMongoClient ¶
NewMongoClient returns Client and it's associated functions
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") 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") 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") deleted, err := client.Delete("test_collection", "1") if err != nil { panic(err) } fmt.Println("Deleted items:", deleted.DeletedCount)
Output:
func (*Client) Get ¶
Get finds one document based on "id" and not "_id"
Example ¶
type data struct { ID int `bson:"id"` Name string `bson:"name"` } client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test") 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" and not "_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") 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") 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") 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") testData := data{ Name: "Akshay", } updated, err := client.Update("test_collection", "1", testData) if err != nil { panic(err) } fmt.Println("Modified items:", updated.ModifiedCount)
Output: