【问题标题】:mongodb mgo custom _idmongodb mgo自定义_id
【发布时间】:2018-11-06 18:09:12
【问题描述】:

我已经使用这种方法来创建/插入一个文档:

document5=({"_id": {"date" : 23, "hour" : 11}, "value" : {"avg_cpu" : 2.558333333333333, "avg_cpu_rate" : 18.419999999999998} })db.userdetails2.insert(document5)

这导致了:

{        "_id" : {                "date" : 23,                "hour" : 11        },        "value" : {                "avg_cpu" : 2.558333333333333,                "avg_cpu_rate" : 18.419999999999998

我将如何使用 golang mgo 做到这一点?

问题是我需要将数据插入到 _id 但外部不可用。

【问题讨论】:

    标签: mongodb mgo


    【解决方案1】:

    如果我已经理解您正在尝试正确执行的操作,那么这应该可以。

    type (
        IdRec struct {
            Date int `bson:"date"`
            Hour int `bson:"hour"`
        }
    
        CpuRec struct {
            AvgCpu     float64 `bson:"avg_cpu"`
            AvgCpuRate float64 `bson:"avg_cpu_rate"`
        }
    
        DocRec struct {
            ID    IdRec  `bson:"_id"`
            Value CpuRec `bson:"value"`
         }
    )
    
        func saveRecord(){
            newRec := DocRec{}
            newRec.ID.date = 23
            newRec.ID.hour = 11
            newRec.Value.AvgCpu = 2.558333333333333
            newRec.Value.AvgCpuRate = 18.419999999999998
    
            session := MongoSession.Copy()
            defer session.Close()
    
            theCollection := session.DB(DbName).C(CollectionName)
            err := theCollection.Insert(&newRec)
            if err != nil {
                doSomething(err)
            }
        }
    

    【讨论】:

    • 谢谢,我会试试的,但是我想出了这个并且它有效: package main ` type UserDetails struct { Id IdType Value ValueType } type IdType struct { Date string Hour string } type ValueType struct { Name string Phone string } ... item := UserDetails{Id: IdType{Date: "23", Hour: "13"}, Value: ValueType{Name: "some-name", Phone: "123 333"}} _, err := col.Upsert(bson.M{"_id": item.Id}, bson.M{"Value": item.Value}) if err != nil { panic(err) }`
    【解决方案2】:

    谢谢,我会尝试的,但是我想出了这个并且它有效:

    package main
    
    import (
        "fmt"
        "time"
    
        "gopkg.in/mgo.v2"
        "gopkg.in/mgo.v2/bson"
    )
    
    //const MongoDb details
    const (
        hosts      = "xxxxx:27017"
        database   = "xxxx"
        username   = "xxxx"
        password   = "xxxx"
        collection = "xxxx"
    )
    
    type UserDetails struct {
        Id    IdType
        Value ValueType
    }
    
    type IdType struct {
        Date string
        Hour string
    }
    
    type ValueType struct {
        Name  string
        Phone string
    }
    
    func main() {
    
        info := &mgo.DialInfo{
            Addrs:    []string{hosts},
            Timeout:  60 * time.Second,
            Database: database,
            Username: username,
            Password: password,
        }
    
        session, err1 := mgo.DialWithInfo(info)
        if err1 != nil {
            panic(err1)
        }
    
        col := session.DB(database).C(collection)
        datab := session.DB(database)
    
        count, err2 := col.Count()
        if err2 != nil {
            panic(err2)
        }
    
        //Write Data//
        item := UserDetails{Id: IdType{Date: "23", Hour: "13"}, Value: ValueType{Name: "some-name", Phone: "123 333"}}
    
        _, err := col.Upsert(bson.M{"_id": item.Id}, bson.M{"Value": item.Value})
    
        if err != nil {
            panic(err)
        }
    
        //Read Data//
        var userDetail []bson.M
        //#1 Search a document with Name= xxxx
        //_ = col.Find(bson.M{"Name": "xxxx"}).All(&userDetail)
    
        //#2 Search All documents
        _ = col.Find(nil).All(&userDetail)
        for _, v := range userDetail {
            fmt.Println(v)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-24
      • 2016-12-15
      • 2012-02-09
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 2016-02-12
      相关资源
      最近更新 更多