Skip to content

Instantly share code, notes, and snippets.

@lidaobing
Created September 29, 2013 09:11
Show Gist options
  • Save lidaobing/6750740 to your computer and use it in GitHub Desktop.
Save lidaobing/6750740 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
)
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
col := session.DB("qbox_test").C("counters")
col.RemoveId("user_userid")
err = col.Insert(bson.M{
"_id": "user_userid",
"n": int64(0),
})
if err != nil {
panic(err)
}
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"n": 1}},
ReturnNew: true,
}
res := map[string]interface{}{}
_, err = col.FindId("user_userid").Apply(change, &res)
if err != nil {
fmt.Printf("%#v\n", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment