Before anything else you should initail the connection ( session )
import"github.com/liontail/mongodb"funcmain() {
...err:=mongodb.InitMongoDB("mongodb://localhost:27017")
//It will initail the singleton of mongo session it will return error if you want to handle it...
}
import"github.com/liontail/mongodb"typeDocumentstruct {
ID bson.ObjectId`json:"id" bson:"_id"`CreatedAt time.Time`json:"created_at" bson:"created_at"`Datastring`json:"data" bson:"data"`
}
}
funcExecuteSomeStuffInMongoDB() {
doc:=Document{
CreatedAt: time.Now(),
Data: "My Document",
}
col:=mongodb.GetMgoCollection("mydb","mycollection")
iferr:=col.Insert(&doc); err!=nil {
//Handle error if you want
}
id:="34yhgvfrtyui213"// id is the hex of the objectIdfindDoc:=Document{}
iferr:=db.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&findDoc); err!=nil {
//Handle error if you want
}
}