Skip to content

Instantly share code, notes, and snippets.

@moongears
Created June 22, 2014 07:16
Show Gist options
  • Save moongears/60744ccc299e68074f75 to your computer and use it in GitHub Desktop.
Save moongears/60744ccc299e68074f75 to your computer and use it in GitHub Desktop.
Golang Mongo remove
package main
import (
"fmt"
"time"
"os"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type Person struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string `bson:"nm"`
Phone string `bson:"ph"`
created time.Time `bson:"c,omitempty"`
}
func main() {
//get session
session, err := mgo.Dial("localhost")
if err != nil {
fmt.Printf("dial fail %v\n", err)
os.Exit(1)
}
defer session.Close()
//error check on every access
session.SetSafe(&mgo.Safe{})
//get collection
coll := session.DB("test_db").C("people")
//remove record
err = coll.Remove(bson.M{"nm": "Stewart Copeland"})
if err != nil {
fmt.Printf("remove fail %v\n", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment