Last active
August 19, 2018 23:32
-
-
Save birowo/019eede1c7bad6daf77211ba880901ff to your computer and use it in GitHub Desktop.
golang latihan crud sederhana mongodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/globalsign/mgo" | |
"github.com/globalsign/mgo/bson" | |
) | |
func FErr(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func SetupDB(url, DBNm string, isDrop bool) (close func(), DB *mgo.Database, err error) { | |
var session *mgo.Session | |
session, err = mgo.Dial(url) | |
close = session.Close | |
session.SetMode(mgo.Monotonic, true) | |
DB = session.DB(DBNm) | |
if isDrop { | |
err = DB.DropDatabase() | |
} | |
return | |
} | |
func NewIds(n int) (ids []bson.ObjectId) { | |
ids = make([]bson.ObjectId, n) | |
for i := 0; i < n; i++ { | |
ids[i] = bson.NewObjectId() | |
} | |
return | |
} | |
func main() { | |
closeSsn, DB, err := SetupDB("localhost", "dbku", true) | |
FErr(err) | |
defer closeSsn() | |
koleksiku := DB.C("koleksiku") | |
var ( | |
rslt bson.M | |
rslts []bson.M | |
) | |
//insert data bertipe bson.M | |
FErr(koleksiku.Insert(bson.M{"field1Name": "1st-field-value", "field2Name": "2nd-field-value"})) | |
FErr(koleksiku.Find(nil).One(&rslt)) // find documents | |
id := rslt["_id"].(bson.ObjectId) | |
FErr(koleksiku.FindId(id).One(&rslt)) // find document by id | |
fmt.Printf("%+v\n", rslt) | |
FErr(koleksiku.Update(bson.M{"_id": id}, bson.M{"field1Name": "1st-field-value-updated", "field2Name": "2nd-field-value-updated"})) | |
FErr(koleksiku.Find(nil).One(&rslt)) | |
fmt.Printf("%+v\n", rslt) | |
FErr(koleksiku.RemoveId(id)) // delete document by id | |
id = bson.NewObjectId() | |
offset := id | |
FErr(koleksiku.Insert(bson.M{"_id": id, "fld1": "1-v1", "fld2": "2-v1"})) | |
ids := NewIds(2) | |
bulk := koleksiku.Bulk() | |
bulk.Insert( // bulk insert | |
bson.M{"_id": ids[0], "fld1": "1-v2", "fld2": "2-v2"}, | |
bson.M{"_id": ids[1], "fld1": "1-v3", "fld2": "2-v3"}, | |
) | |
_, err = bulk.Run() | |
FErr(err) | |
type Model1 struct { | |
ID bson.ObjectId `bson:"_id"` | |
Field1 string `bson:"fld1"` | |
Field2 string `bson:"fld2"` | |
} | |
id = bson.NewObjectId() | |
// insert data jika bertipe struct maka harus menggunakan tag bson: | |
// https://github.com/golang/go/wiki/Well-known-struct-tags#list-of-well-known-struct-tags | |
// https://medium.com/golangspec/tags-in-golang-3e5db0b8ef3e | |
FErr(koleksiku.Insert(Model1{id, "1-v4", "2-v4"})) | |
FErr(koleksiku.Find(nil).All(&rslts)) // find all documents | |
fmt.Printf("%+v\n", rslts) | |
limit := 2 | |
rsltMdl1 := make([]Model1, limit) | |
// cari docs dimana _id > offset , jumlah docs hasil sebatas limit | |
// hasil tampung di var bertipe slice of Model1 | |
FErr(koleksiku.Find(bson.M{"_id": bson.M{"$gt": offset}}).Limit(limit).All(&rsltMdl1)) | |
fmt.Printf("%+v\n", rsltMdl1) | |
} | |
/* | |
catatan : | |
- istilah: table di sql jadi collection di mgo , row di sql jadi document di mgo | |
- tidak ada semacam CREATE DATABASE & CREATE TABLE di mgo , | |
- database & collection di mgo jika belum ada / sudah di - drop akan dibuat saat pertama kali ada proses insert | |
- di sql kita tahu bahwa field - field harus dibuat (create table) dahulu sebelum bisa insert data , | |
- tetapi di mgo , field - field dibuat bersamaan dengan value data - data - nya ketika insert | |
- di mgo document , field _id selalu ada dan otomatis di - index untuk pencarian | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment