zoukankan      html  css  js  c++  java
  • golang-mongodb范例

      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         return
    104     }
    105     //
    106     session.Close()
    107 }
  • 相关阅读:
    093孤荷凌寒自学第179天区块链093天Dapp048
    092孤荷凌寒自学第178天区块链092Dapp047
    091孤荷凌寒自学第177天区块链091天Dapp046
    090孤荷凌寒自学第180天区块链090天Dapp045
    089孤荷凌寒自学第175天区块链089天Dapp044
    088孤荷凌寒自学第174天区块链088天Dapp043
    087孤荷凌寒自学第173天区块链087天Dapp042
    孤荷凌寒自学第172天区块链086天Dapp041
    孤荷凌寒自学第171天区块链085天Dapp040
    iOS开发——高级篇——内存分析,Instruments
  • 原文地址:https://www.cnblogs.com/--xiaoyao--/p/5284685.html
Copyright © 2011-2022 走看看