mongodb

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

README

Go MongoDB


Description

This is a simple package to connect to a MongoDB database.

Connection and Get Collection Usage

package main

import (
    "github.com/root27/go-mongodb"
)

func main(){
     

	db ,err:= mongodb.Connect("mongourl") // mongourl is the env variable name

    if err != nil {
        panic(err)
    }

    // db is a pointer to a mongo.Database
    // use it to perform operations on the database

    //Get Collection
    collection := db.GetCollection(db, "databaseName","collectionName")
}

Insert Usage

package main

import (
    "github.com/root27/go-mongodb"
)

func main(){
     

    db ,err:= mongodb.Connect("mongourl") // mongourl is the env variable name

    if err != nil {
        panic(err)
    }

    // db is a pointer to a mongo.Database
    // use it to perform operations on the database

    //Get Collection
    collection := db.GetCollection(db, "databaseName","collectionName")

    //Insert
    err = collection.InsertOne(db, bson.M{"name": "pi", "value": 3.14159})
    if err != nil {
        panic(err)
    }
}

Find Usage


package main

import (
    "github.com/root27/go-mongodb"
)

func main(){
     

    db ,err:= mongodb.Connect("mongourl") // mongourl is the env variable name

    if err != nil {
        panic(err)
    }

    // db is a pointer to a mongo.Database
    // use it to perform operations on the database

    //Get Collection
    collection := db.GetCollection(db, "databaseName","collectionName")

    //Find
    var result bson.M
    err = collection.FindOne(db, bson.M{"name": "pi"}).Decode(&result)
    if err != nil {
        panic(err)
    }

    fmt.Println("pi:", result["value"])
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(envName string) (*mongo.Client, error)

func DeleteOne added in v0.0.6

func DeleteOne(collection *mongo.Collection, filter interface{}) (*mongo.DeleteResult, error)

func Disconnect added in v0.0.4

func Disconnect(client *mongo.Client)

func FindMany added in v0.0.7

func FindMany(collection *mongo.Collection, filter interface{}) (*mongo.Cursor, error)

func FindOne added in v0.0.4

func FindOne(collection *mongo.Collection, filter interface{}) *mongo.SingleResult

func GetCollection added in v0.0.3

func GetCollection(client *mongo.Client, dbName string, collectionName string) *mongo.Collection

func InsertMany added in v0.0.4

func InsertMany(collection *mongo.Collection, data []interface{}) (*mongo.InsertManyResult, error)

func InsertOne added in v0.0.4

func InsertOne(collection *mongo.Collection, data interface{}) (*mongo.InsertOneResult, error)

func LoadEnv

func LoadEnv(envName string) string

func UpdateMany added in v0.0.7

func UpdateMany(collection mongo.Collection, filter interface{}, update interface{}) (*mongo.UpdateResult, error)

func UpdateOne added in v0.0.6

func UpdateOne(collection mongo.Collection, filter interface{}, update interface{}) (*mongo.UpdateResult, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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