golang-mongodb範例

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
  1 package main  2   3 import (  4     "log"  5   6     "gopkg.in/mgo.v2"  7     "gopkg.in/mgo.v2/bson"  8 )  9  10 type Address struct { 11     Address string 12 } 13 type Location struct { 14     Longitude float64 15     Latitude  float64 16 } 17  18 type Person struct { 19     Id       bson.ObjectId `bson:"_id"` 20     Name     string 21     Age_Int  int  22     Address  []Address 23     Location Location 24 } 25  26 func main() { 27     log.SetFlags(log.Flags() | log.Lshortfile) 28     //串連 29     session, err := mgo.Dial("127.0.0.1:27017") 30     if err != nil { 31         log.Println(err) 32         return 33     }    34     //設定模式 35     session.SetMode(mgo.Monotonic, true) 36     //擷取文檔集 37     collection := session.DB("test").C("person") 38     // 建立索引 39     index := mgo.Index{ 40         Key:        []string{"name"}, // 索引欄位, 預設升序,若需降序在欄位前加- 41         Unique:     true,             // 唯一索引 同mysql唯一索引 42         DropDups:   true,             // 索引重複替換舊文檔,Unique為true時失效 43         Background: true,             // 後台建立索引 44     }    45     if err := collection.EnsureIndex(index); err != nil { 46         log.Println(err) 47         return 48     }    49     if err := collection.EnsureIndexKey("$2dsphere:location"); err != nil { // 建立一個範圍索引 50         log.Println(err) 51         return 52     }    53     //添加記錄 54     person := Person{ 55         Id:      bson.NewObjectId(), 56         Name:    "逍遙", 57         Age_Int: 24,  58         Address: []Address{ 59             Address{ 60                 Address: "仙靈島", 61             },   62         },   63         Location: Location{ 64            Longitude: 1, 65             Latitude:  1, 66         }, 67     } 68     if err := collection.Insert(person); err != nil { 69         log.Println(err) 70         return 71     } 72     //尋找記錄 73     newPerson := &Person{} 74     if err := collection.Find(bson.M{"age_int": 24}).One(newPerson); err != nil { 75         log.Println(err) 76         return 77     } 78     //修改記錄 79     if err := collection.Update(bson.M{"age_int": 24}, bson.M{"$set": bson.M{"age_int": 26}}); err != nil { 80         log.Println(err) 81         return 82     } 83     //刪除記錄 84     //if err := collection.Remove(bson.M{"age_int": 26}); err != nil { 85     //  log.Println(err) 86     //  return 87     //} 88     //位置搜尋 89     selector := bson.M{ 90         "location": bson.M{ 91             "$near": bson.M{ 92                 "$geometry": bson.M{ 93                     "type":        "Point", 94                     "coordinates": []float64{1, 1}, 95                 }, 96                 "$maxDistance": 1, 97                 //"$minDistance": 0, 98             }, 99         },100     }101     if err := collection.Find(selector).One(newPerson); err != nil {102         log.Println(err)103         return104     }105     //106     session.Close()107 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.