Mongo DBMS client. The package aims to match Express seamless integration with NodeJs and MongoDb.
- MongoDb (access to a server)
- Mgo
(GoDeps supported as well.) Manually:
go get github.com/asaskevich/govalidator
go get gopkg.in/mgo.v2
go get github.com/cheikhshift/db
import (
"log" //optional
"github.com/cheikhshift/db"
)
The snippet below will connect to your database
@ your localhost
dbs,err := db.Connect("localhost","database")
if err != nil {
log.Fatal(err)
}
Format of server URI :
[mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
Throuughout this page the statement model to set query
is used. It refers to the interface you wish to query. This works by using the specified interface as a table/collection name.
Define a new interface as usual (fields Created and Id are system generated) :
type MyObject struct {
Id bson.ObjectId `bson:"_id,omitempty"`
TestField string `valid:"unique"`
FieldTwo string `valid:"email,required"`
Created time.Time //timestamp local format
}
The following tag will ensure no duplicates happen while saving.
TestField string `valid:"unique"`
The following tag valid
will ensure your field data is an email.
FieldTwo string `valid:"email,required"`
Validation possible by package Go Validator. Visit the repository for a full list of supported validator interface tags.
obj := NewObject{FieldTwo: "value" }
dbs.New(&obj)
err = dbs.Save(&obj)
if err != nil {
log.Fatal(err)
}
The following query will return one object. Within the Find function you may use all of the usual Mongo DB query functions. Remember to add strings even around $
prefixed parameters. The following example will retrieve one result. Any interface field is converted to lower case on database save, when performing a query type interface field names without capital letters.
query := MyObject{}
dbs.Q(query).Find(db.O{"fieldtwo":"value"}).One(&query)
fmt.Println(query)
This function will fail if your interface's Id
field is blank.
obj := NewObject{FieldTwo: "value" }
err := dbs.Add(&obj)
if err != nil {
log.Fatal(err)
}
dbs.Remove(&obj)
#Index
package github.com/cheikhshift/db
type DB struct {
MoDb *mgo.Database
}
type O bson.M
Create a new database connection.
func Connect(url string, db string) (DB,error)
-url : MongoDb server URI to connect to.
- db: database to use.
Close database connection.
func (d DB) Close()
Verify if model is valid. This package is used for validation, Read more about supported types here.
func (d DB) PreVerify(item interface{}) error
- item : Model (struct) to verify.
More information about mgo.Query here. Once your query is retrieved you may count ,limit, skip as you please.
func (d DB) Query(item interface{}, query interface{}) *mgo.Query
- item : Model to set query to.
- query :
db.O
map of parameters to query.
Add the specified models to your database.
func (d DB) Add(items ...interface{}) error {
- items : models to save.
Update the model.
func (d DB) Save(item interface{}) error
- item : model to update.
Remove model from database. If the model has no value for field Id this function will fail.
func (d DB) Remove(items ...interface{}) error
- items : items to remove.
Remove items via db.O
query.
func (d DB) RemoveAll(item interface{},query interface{}) (*mgo.ChangeInfo,error)
- item : model to set query to.
- query : map of parameters to query.
Update models in your database via query.
func (d DB) UpdateAll(item interface{},query interface{}, update interface{}) (*mgo.ChangeInfo,error)
- item : model to set query to.
- query : map of parameters to query.
- update : map of updates to make. Remember to use MongoDb selections , ie :
$ne
.