-
-
Save lequi/dd578b7c47e692b122aedcaa0108fb24 to your computer and use it in GitHub Desktop.
Revisions
-
congjf revised this gist
Dec 27, 2013 . 1 changed file with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) } } -
congjf revised this gist
Dec 23, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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{"$exist": 1}} result, _ := org.Retrieve(conditions) fmt.Fprint(rw, result) -
congjf revised this gist
Dec 23, 2013 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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() -
congjf revised this gist
Dec 23, 2013 . 1 changed file with 55 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 } -
congjf renamed this gist
Dec 19, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
congjf revised this gist
Dec 19, 2013 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) -
congjf revised this gist
Dec 19, 2013 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Using MongoDB in golang with mgo -
border revised this gist
Aug 27, 2012 . 1 changed file with 11 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,21 +2,21 @@ package main import ( "fmt" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" "time" ) 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") @@ -41,11 +41,11 @@ func main() { // Index index := mgo.Index{ Key: []string{"name", "phone"}, Unique: true, DropDups: true, Background: 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) } -
border created this gist
Aug 27, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) }