mongo

package module
v0.0.0-...-12438d0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2018 License: MIT Imports: 6 Imported by: 1

README

Mongo

GoDoc

The mongo package is a very simple wrapper around the github.com/globalsign/mgo package. It's purpose is to allow you to do CRUD operations with very little code. It's not exhaustive and not meant to do everything for you.

License

Mongo is licensed under the MIT license.

Installation

To install mongo, simply run go get github.com/sfreiberg/mongo.

Example

package main

import (
	"github.com/sfreiberg/mongo"
	"github.com/globalsign/mgo/bson"
)

type Customer struct {
	Id        bson.ObjectId `bson:"_id"`
	Firstname string
	Lastname  string
}

func init() {
	// Set server (localhost) and database (MyApp)
	err := mongo.SetServers("localhost", "MyApp")
	if err != nil {
		panic(err)
	}
}

func main() {
	customers := []interface{}{
		&Customer{
			Firstname: "George",
			Lastname:  "Jetson",
		},
		&Customer{
			Firstname: "Judy",
			Lastname:  "Jetson",
		},
	}

	err := mongo.Insert(customers...)
	if err != nil {
		panic(err)
	}
}

Documentation

Overview

The mongo package is a very simple wrapper around the github.com/globalsign/mgo/bson package. It's purpose is to allow you to do CRUD operations with very little code. It's not exhaustive and not meant to do everything for you.

Index

Constants

This section is empty.

Variables

View Source
var (
	NoPtr = errors.New("You must pass in a pointer")
)

Functions

func Count

func Count(i interface{}) (int, error)

Does a count on the collection for the struct that is passed in.

func Delete

func Delete(i interface{}) error

Deletes a record. Uses the Id to identify the record to delete. Must pass in a pointer to a struct.

func Find

func Find(i interface{}, q bson.M, sortFields ...string) error

Find one or more records. If a single struct is passed in we'll return one record. If a slice is passed in all records will be returned. Must pass in a pointer to a struct or slice of structs. Use sortFields to sort the results. See http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order for more info.

func FindById

func FindById(i interface{}, id string) error

Find a single record by id. Must pass a pointer to a struct.

func GetColl

func GetColl(session *mgo.Session, coll string) *mgo.Collection

We pass in the session because that is a clone of the original and the caller will need to close it when finished.

func GetSession

func GetSession() (*mgo.Session, error)

Returns a Mongo session. You must call Session.Close() when you're done.

func Insert

func Insert(records ...interface{}) error

Insert one or more structs. Must pass in a pointer to a struct. The struct must contain an Id field of type bson.ObjectId with a tag of `bson:"_id"`.

func SetServers

func SetServers(servers, db string) error

Set the mongo servers and the database

func Update

func Update(i interface{}) error

Updates a record. Uses the Id to identify the record to update. Must pass in a pointer to a struct.

Types

type Id

type Id string

Id let's you use a string data type for your models instead of the native bson.ObjectId. The main benefit is when you frequently want a hex represenation such as for use in web apps. You still need to provide the `bson:"_id"` tag.

func (Id) GetBSON

func (i Id) GetBSON() (interface{}, error)

func (*Id) SetBSON

func (i *Id) SetBSON(raw bson.Raw) error

Jump to

Keyboard shortcuts

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