Skip to content

Instantly share code, notes, and snippets.

@lequi
Forked from congjf/.Title
Created September 24, 2016 08:15
Show Gist options
  • Save lequi/dd578b7c47e692b122aedcaa0108fb24 to your computer and use it in GitHub Desktop.
Save lequi/dd578b7c47e692b122aedcaa0108fb24 to your computer and use it in GitHub Desktop.

Revisions

  1. @congjf congjf revised this gist Dec 27, 2013. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions 5.RetrieveFiled.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    func (u Organizations) Index(rw http.ResponseWriter, req *http.Request) {
    notice := "Organizations#Index:" + req.Method + req.URL.String()
    log.Println(notice)
    values := req.URL.Query()

    var org model.Organizations
    conditions := bson.M{"_id": bson.M{"$exists": true}}

    if values.Get("name") != "" {
    conditions["name"] = values.Get("name")
    }
    if values.Get("area") != "" {
    conditions["area"] = values.Get("area")
    }
    if values.Get("type") != "" {
    conditions["type"] = values.Get("type")
    }

    result, _ := org.Retrieve(conditions)
    encoder := json.NewEncoder(rw)
    err := encoder.Encode(result)
    if err != nil {
    log.Println(err.Error())
    rw.WriteHeader(http.StatusInternalServerError)
    }
    }
  2. @congjf congjf revised this gist Dec 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 3. Find_All.go
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ type Organizations struct {
    func (u *Organizations) Index(rw http.ResponseWriter, req *http.Request) {
    var org Organizations

    conditions := bson.M{"_id": bson.M{"$ne": ""}}
    conditions := bson.M{"_id": bson.M{"$exist": 1}}

    result, _ := org.Retrieve(conditions)
    fmt.Fprint(rw, result)
  3. @congjf congjf revised this gist Dec 23, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions 4. Return_Array_Only.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    // The projection parameter specifies which fields to return.
    // http://docs.mongodb.org/manual/reference/method/db.collection.find/#projections
    // db.products.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 } )

    // Or Query().select()
  4. @congjf congjf revised this gist Dec 23, 2013. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions 3. Find_All.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import (
    // native packages
    "encoding/json"
    "fmt"
    "log"
    "net/http"

    // 3rd packages
    "labix.org/v2/mgo/bson"
    )


    // Organizations数据模型结构
    type Organizations struct {
    Name string `json:"name"`
    Area string `json:"area"`
    Type string `json:"type"`
    CustomerType string `json:"customertype"`
    State string `json:"state"`
    LastUpdated string `json:"lastupdated"`
    Assigned string `json:"assigned"`

    Address `json:"address"`
    }

    // for GET /Organizations
    func (u *Organizations) Index(rw http.ResponseWriter, req *http.Request) {
    var org Organizations

    conditions := bson.M{"_id": bson.M{"$ne": ""}}

    result, _ := org.Retrieve(conditions)
    fmt.Fprint(rw, result)
    }

    // Organizations Find
    func (o *Organizations) Retrieve(conditions map[string]interface{}) ([]Organizations, error) {
    session, err := getSession()
    if err != nil {
    return nil, err
    }
    defer session.Close()

    collection := session.DB(DATABASE).C("organizations")

    result := []Organizations{}

    err = collection.Find(conditions).All(&result)
    if err != nil {
    log.Println(err.Error())
    return nil, err
    }

    return result, nil
    }
  5. @congjf congjf renamed this gist Dec 19, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @congjf congjf revised this gist Dec 19, 2013. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions mgoExample.go
    Original file line number Diff line number Diff line change
    @@ -85,6 +85,14 @@ func main() {
    if err != nil {
    panic(err)
    }

    // Push a item to the Array in the Collection by Collection's ObjectId
    idQueryier := bson.ObjectIdHex("52b298f8b6bb960ff805ef3b")
    change := bson.M{"$push": bson.M{"sections": bson.M{"name":"office"}}}
    err = c.Update(idQuerier, change)
    if err != nil {
    panic(err)
    }

    // Query All
    err = c.Find(bson.M{"name": "Ale"}).Sort("-timestamp").All(&results)
  7. @congjf congjf revised this gist Dec 19, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions .Title
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Using MongoDB in golang with mgo
  8. @border border revised this gist Aug 27, 2012. 1 changed file with 11 additions and 13 deletions.
    24 changes: 11 additions & 13 deletions mgoExample.go
    Original file line number Diff line number Diff line change
    @@ -2,21 +2,21 @@ package main

    import (
    "fmt"
    "time"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
    "time"
    )

    type Person struct {
    ID bson.ObjectId `bson:"_id,omitempty"`
    Name string
    Phone string
    Name string
    Phone string
    Timestamp time.Time
    }

    var (
    IsDrop = true
    )
    IsDrop = true
    )

    func main() {
    session, err := mgo.Dial("127.0.0.1")
    @@ -41,11 +41,11 @@ func main() {

    // Index
    index := mgo.Index{
    Key: []string{"name", "phone"},
    Unique: true,
    DropDups: true,
    Key: []string{"name", "phone"},
    Unique: true,
    DropDups: true,
    Background: true,
    Sparse: true,
    Sparse: true,
    }

    err = c.EnsureIndex(index)
    @@ -56,7 +56,7 @@ func main() {
    // Insert Datas
    err = c.Insert(&Person{Name: "Ale", Phone: "+55 53 1234 4321", Timestamp: time.Now()},
    &Person{Name: "Cla", Phone: "+66 33 1234 5678", Timestamp: time.Now()})

    if err != nil {
    panic(err)
    }
    @@ -85,7 +85,6 @@ func main() {
    if err != nil {
    panic(err)
    }


    // Query All
    err = c.Find(bson.M{"name": "Ale"}).Sort("-timestamp").All(&results)
    @@ -94,6 +93,5 @@ func main() {
    panic(err)
    }
    fmt.Println("Results All: ", results)


    }
    }
  9. @border border created this gist Aug 27, 2012.
    99 changes: 99 additions & 0 deletions mgoExample.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    package main

    import (
    "fmt"
    "time"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
    )

    type Person struct {
    ID bson.ObjectId `bson:"_id,omitempty"`
    Name string
    Phone string
    Timestamp time.Time
    }

    var (
    IsDrop = true
    )

    func main() {
    session, err := mgo.Dial("127.0.0.1")
    if err != nil {
    panic(err)
    }

    defer session.Close()

    session.SetMode(mgo.Monotonic, true)

    // Drop Database
    if IsDrop {
    err = session.DB("test").DropDatabase()
    if err != nil {
    panic(err)
    }
    }

    // Collection People
    c := session.DB("test").C("people")

    // Index
    index := mgo.Index{
    Key: []string{"name", "phone"},
    Unique: true,
    DropDups: true,
    Background: true,
    Sparse: true,
    }

    err = c.EnsureIndex(index)
    if err != nil {
    panic(err)
    }

    // Insert Datas
    err = c.Insert(&Person{Name: "Ale", Phone: "+55 53 1234 4321", Timestamp: time.Now()},
    &Person{Name: "Cla", Phone: "+66 33 1234 5678", Timestamp: time.Now()})

    if err != nil {
    panic(err)
    }

    // Query One
    result := Person{}
    err = c.Find(bson.M{"name": "Ale"}).Select(bson.M{"phone": 0}).One(&result)
    if err != nil {
    panic(err)
    }
    fmt.Println("Phone", result)

    // Query All
    var results []Person
    err = c.Find(bson.M{"name": "Ale"}).Sort("-timestamp").All(&results)

    if err != nil {
    panic(err)
    }
    fmt.Println("Results All: ", results)

    // Update
    colQuerier := bson.M{"name": "Ale"}
    change := bson.M{"$set": bson.M{"phone": "+86 99 8888 7777", "timestamp": time.Now()}}
    err = c.Update(colQuerier, change)
    if err != nil {
    panic(err)
    }


    // Query All
    err = c.Find(bson.M{"name": "Ale"}).Sort("-timestamp").All(&results)

    if err != nil {
    panic(err)
    }
    fmt.Println("Results All: ", results)


    }