mhutter / mr

Go library for interaction with MongoDB

Home Page:https://gowalker.org/github.com/mhutter/mr

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MongoRepo

GoDoc Build Status

Library to ease interaction with MongoDB.

This package is an opinionated abstraction for mgo.

To get started, connect to MongoDB (uses $MONGODB_URI from environment if set, or localhost):

repo := mr.MustAutoconnect("db-name")

Include the "Base" type in your data types:

type User struct {
        mr.Base `json:",inline" bson:",inline"`

        Name         string `bson:"name" json:"name"`
        Email        string `bson:"email" json:"email"`
        PasswordHash []byte `bson:"password_hash" json:"-"`
}

Doing so will add the following fields to your type:

  • ID: ObjectId of the document in the DB
  • CreatedAt: Timestamp of first insertion into DB
  • UpdatedAt: Timestamp of the last update operations

The "MongoCollection" type implements the basic CRUD operations defined in the "Colleciton" interface. IDs ,must be passed as strings, and objects are usually types that include the "Base" type (see above).

user := User{
        Name: "John",
}
err := repo.Insert(&user)

var users User
err := repo.FindAll(&users)

Manuel Hutter - GitHub @mhutter - Twitter @dratir

About

Go library for interaction with MongoDB

https://gowalker.org/github.com/mhutter/mr

License:MIT License


Languages

Language:Go 100.0%