dragonhong641016 2016-08-25 19:34
浏览 1315

使用bson.NewObjectID()使用自我生成的_Id进行Golang Mongo插入会导致意外的模式

Hi i am using mongo and golang according to my use case i want to generate a _id before insertion, for that i am using

 bson.NewobjectId()

my struct is somewhat like this

type Device struct {
    Id             bson.ObjectId `bson:"_id" json:"_id,omitempty"`
    UserId         string        `bson:"userId" json:"userId"`
    CategorySlug   string        `bson:"categorySlug" json:"categorySlug"`
    CreatedAt      time.Time     `bson:"createdAt" json:"createdAt"`
    ModifiedAt     time.Time     `bson:"modifiedAt" json:"modifiedAt"`
    BrandId        int           `bson:"brandId" json:"brandId"`
    Category       string        `bson:"category" json:"category"`
}

when i am using this json request

{
    "userId" : "gKn42jJD8uy8ksQpi",
    "categorySlug" : "television",
    "createdAt" : "2016-08-25T18:47:29.558Z",
    "modifiedAt" : "2016-08-25T18:47:29.558Z",
    "brandId" : 90,
    "category" : "LED TV",
    "dateOfPurchase" : "2016-08-25T18:47:29.558Z"
}

and decodes this into device type and after that initialize my id using decode.ID = bson.NewObjectId()

but when i looked into my database the inserted value its strangely in this form

{
    "_id" : ObjectId("57bf425a34ce5ee85891b914"),
    "0" : {
        "_id" : ObjectId("57bf425ae03ec2179a000001"),
        "userId" : "gKn42jJD8uy8ksQpi",
        "categorySlug" : "television",
        "createdAt" : ISODate("2016-08-25T18:47:29.558Z"),
        "modifiedAt" : ISODate("2016-08-25T18:47:29.558Z"),
        "brandId" : 90,
        "category" : "LED TV",
        "dateofpurchase" : ISODate("2016-08-25T18:47:29.558Z")
    }
}

I dont know the reason why is this happening so i want my data in a proper mongo document . Please help me let know why is this thing happening and new _id is getting generated

  • 写回答

1条回答 默认 最新

  • douchen7555 2016-08-25 22:33
    关注

    I'm pretty sure it's something with your other code. Next code (that uses your data structure) correctly inserts single entries.

    package main
    
    import (
        "gopkg.in/mgo.v2"
        "gopkg.in/mgo.v2/bson"
        "time"
    )
    
    type Device struct {
        Id           bson.ObjectId `bson:"_id" json:"_id,omitempty"`
        UserId       string        `bson:"userId" json:"userId"`
        CategorySlug string        `bson:"categorySlug" json:"categorySlug"`
        CreatedAt    time.Time     `bson:"createdAt" json:"createdAt"`
        ModifiedAt   time.Time     `bson:"modifiedAt" json:"modifiedAt"`
        BrandId      int           `bson:"brandId" json:"brandId"`
        Category     string        `bson:"category" json:"category"`
    }
    
    func main() {
        session, err := mgo.Dial("mongodb://127.0.0.1:27017/so")
    
        if err != nil {
            panic(err)
        }
        defer session.Close()
        session.SetMode(mgo.Monotonic, true)
    
        c := session.DB("so").C("insrt")
    
        doc := Device{
            Id:           bson.NewObjectId(),
            UserId:       "gKn42jJD8uy8ksQpi",
            CategorySlug: "television",
            CreatedAt:    time.Now(),
            ModifiedAt:   time.Now(),
            BrandId:      90,
            Category:     "LED TV",
        }
    
        err = c.Insert(doc)
        if err != nil {
            panic(err)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP