美文网首页
Golang 与mongodb的交互。嵌套struct

Golang 与mongodb的交互。嵌套struct

作者: 毛毛v5 | 来源:发表于2019-07-16 15:59 被阅读0次

    mongodb这类nosql数据库,还是先定义结构比较好啊。存取方便。蛋疼的是golang定义个嵌套的struct好费劲啊。。
    primitive.NewObjectID() 可以生成一个mongodb的_id,找了半天这个函数。网上都是 mgo的函数。好像好久不更新了。mongo-driver是mongodb的官方驱动?所以不用第三方的了吧。

    package books
    
    import (
        "context"
        "fmt"
    
        "go.mongodb.org/mongo-driver/bson"
        "go.mongodb.org/mongo-driver/mongo"
        "go.mongodb.org/mongo-driver/mongo/options"
    
        //////////////"go.mongodb.org/mongo-driver/mongo/options"
        "go.mongodb.org/mongo-driver/bson/primitive"
    )
    
    var (
        cctn *mongo.Collection
    )
    
    type ReaderInfo struct {
        UserID   int64  `json:"userid" bson:"userid"`
        NickName string `json:"nickname" bson:"nickname"`
    }
    
    type ProBlockInfo struct {
        L int64 `json:"l" bson:"l"`
        T int64 `json:"t" bson:"t"`
        R int64 `json:"r" bson:"r"`
        B int64 `json:"b" bson:"b"`
    }
    
    type PageInfo struct {
        MPic       string         `json:"mpic" bson:"mpic"`
        CreateTime int64          `json:"createtime" bson:"createtime"`
        ProBlock   []ProBlockInfo `json:"problock" bson:"problock"`
        BPic       []string       `json:"bpic" bson:"bpic"`
    }
    
    type BookInfo struct {
        ID          primitive.ObjectID `json:"id" bson:"_id"`
        UserID      int64              `json:"userid" bson:"userid"`
        CreateTime  int64              `json:"createtime" bson:"createtime"`
        Name        string             `json:"name" bson:"name"`
        PageNum     int64              `json:"pagenum" bson:"pagenum"`
        Author      string             `json:"author" bson:"author"`
        AuthorTime  int64              `json:"authortime" bson:"authortime"`
        PublishTime int64              `json:"publishtime" bson:"publishtime"`
        ISBN        string             `json:"isbn" bson:"isbn"`
        Publisher   string             `json:"publisher" bson:"publisher"`
        Readed      int64              `json:"readed" bson:"readed"`
        Liked       int64              `json:"liked" bson:"liked"`
        Reader      []ReaderInfo       `json:"reader" bson:"reader"`
        Liker       []ReaderInfo       `json:"liker" bson:"liker"`
        Tags        []string           `json:"tags" bson:"tags"`
        Pages       []PageInfo         `json:"pages" bson:"pages"`
    }
    
    func Init(client *mongo.Client) {
        cctn = client.Database("Shot4Read").Collection("books")
    }
    
    func GetBooks(startIdx int64, pageSize int64) {
        if cursor, err := cctn.Find(context.Background(), bson.M{}, options.Find().SetLimit(pageSize).SetSkip(startIdx), options.Find().SetSort(bson.M{"CreateTime": -1})); err == nil {
            defer cursor.Close(context.Background())
            for cursor.Next(context.Background()) {
                //var howie interface{}
                var books BookInfo
                if err = cursor.Decode(&books); err == nil {
                    fmt.Printf("Find查询到的数据:%v\n", books)
                }
    
                //howieArrayEmpty = append(howieArrayEmpty, howie)
            }
        } else {
            fmt.Println(err)
        }
    }
    
    func InsertTestData() {
        bi := BookInfo{}
        bi.ID = primitive.NewObjectID()
        bi.Name = "hello"
        bi.UserID = 3002
        reader := ReaderInfo{}
        reader.UserID = 30008
        reader.NickName = "停车坐爱枫林晚"
        bi.Reader = append(bi.Reader, reader)
        liker := ReaderInfo{}
        liker.UserID = 30008
        liker.NickName = "停车坐爱枫林晚"
        bi.Liker = append(bi.Liker, liker)
        bi.Tags = append(bi.Tags, "高数")
        bi.Tags = append(bi.Tags, "高等数学")
        page := PageInfo{}
        pb := ProBlockInfo{}
        pb.L = 20
        pb.T = 40
        pb.R = 220
        pb.B = 200
        page.ProBlock = append(page.ProBlock, pb)
        page.BPic = append(page.BPic, "http://www.pic98.com/abc.png")
        bi.Pages = append(bi.Pages, page)
    
        r, err := cctn.InsertOne(context.Background(), bi)
        fmt.Println(r, err)
    
    }
    
    

    相关文章

      网友评论

          本文标题:Golang 与mongodb的交互。嵌套struct

          本文链接:https://www.haomeiwen.com/subject/njstlctx.html