dqmfo84644 2019-06-14 03:13
浏览 332

如何使用官方MongoDB Go驱动程序的FindOne()函数从mongoDB获取完整文档

I am trying to get a single document from MongoDB and Decode it into a struct that contains slice. I use the official MongoDB Go driver.

I've tried collection.FindOne(), that returns everything but slices, and collection.Find() that returns EOF.

Here's a simple FindOne() function:

func FindOne(c *mongo.Collection, filter, result interface{}, opts ...*options.FindOneOptions) error {

    err := c.FindOne(context.TODO(), filter, opts...).Decode(result)
    if err != nil {
        return err
    }
    return nil
}

I'm trying to decode result into structs:

type UserBonus struct {
    Id                   string          `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    User                 *User           `protobuf:"bytes,2,opt,name=user,proto3" json:"user,omitempty"`
    Bonus                []*Bonus        `protobuf:"bytes,3,rep,name=bonus,proto3" json:"bonus,omitempty"`
    Payments             []*BonusPayment `protobuf:"bytes,4,rep,name=payments,proto3" json:"payments,omitempty"`
}

type Bonus struct {
    Id                   string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    DueDate              int64    `protobuf:"varint,2,opt,name=dueDate,proto3" json:"dueDate,omitempty"`
    CancelDate           int64    `protobuf:"varint,3,opt,name=cancelDate,proto3" json:"cancelDate,omitempty"`
    LastUpdate           int64    `protobuf:"varint,4,opt,name=lastUpdate,proto3" json:"lastUpdate,omitempty"`
    Amount               float32  `protobuf:"fixed32,5,opt,name=amount,proto3" json:"amount,omitempty"`
}

type BonusPayment struct {
    Id                   string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    ProductId            int64    `protobuf:"varint,2,opt,name=productId,proto3" json:"productId,omitempty"`
    Amount               float32  `protobuf:"fixed32,3,opt,name=amount,proto3" json:"amount,omitempty"`
    CreateDate           int64    `protobuf:"varint,4,opt,name=createDate,proto3" json:"createDate,omitempty"`
    CancelDate           int64    `protobuf:"varint,5,opt,name=cancelDate,proto3" json:"cancelDate,omitempty"`
    LastUpdate           int64    `protobuf:"varint,6,opt,name=lastUpdate,proto3" json:"lastUpdate,omitempty"`
}
col := mng.GetCollection(db, "bonus")
filter := bson.M{"id":"duma@mail.com"}

var result pb.UserBonus

err = mng.FindOne(col, filter, &result)

But the result is

{"id":"duma@mail.com","user":{"id":"duma@mail.com","firstName":"Comte","lastName":"de La Fère","email":"duma@mail.com"}}

without []*Bonus and []*BonusPayment, but if I open terminal it shows me full result

db.bonus.findOne({"id":"duma@mail.com"})
{
    "_id" : ObjectId("5d030aadf464d3397c73ec32"),
    "id" : "duma@mail.com",
    "user" : {
        "id" : "duma@mail.com",
        "firstname" : "Comte",
        "lastname" : "de La Fère",
        "email" : "duma@mail.com",
        "balance" : 134.5999984741211
    },
    "bonus" : [
        {
            "id" : "",
            "duedate" : NumberLong(1560480429),
            "canceldate" : NumberLong(0),
            "lastupdate" : NumberLong(1560480429),
            "amount" : 50
        },
        {
            "createDate" : NumberLong(1560480429),
            "lastUpdate" : NumberLong(1560480429),
            "amount" : 34.5
        }
    ],
    "lastbonusid" : NumberLong(0),
    "payments" : [
        {
            "id" : "",
            "productid" : NumberLong(1),
            "amount" : 10,
            "createdate" : NumberLong(0),
            "canceldate" : NumberLong(0),
            "lastupdate" : NumberLong(0)
        }
    ]
}

Found the issue. If I after inserting I run update query

update := bson.M{
            "$inc": bson.M{"user.balance": input.Bonus.Amount},
    }

It stops bringing arrays and user.balance, but it's a different issue. Thanks, everybody who tried to help.

  • 写回答

1条回答 默认 最新

  • douyuben9434 2019-06-14 19:24
    关注

    i have some project using the old community driver (mgo), and i faced this problem. My solution was to add a tag 'bson' in my Struct's Fields.

    type UserBonus struct {
        Id                   string          `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
        User                 *User           `protobuf:"bytes,2,opt,name=user,proto3" json:"user,omitempty"`
        Bonus                []*Bonus        `protobuf:"bytes,3,rep,name=bonus,proto3" json:"bonus,omitempty" bson:"bonus,omitempty"`
        Payments             []*BonusPayment `protobuf:"bytes,4,rep,name=payments,proto3" json:"payments,omitempty" bson:"payments,omitempty"`
    }
    
    type Bonus struct {
        Id                   string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
        DueDate              int64    `protobuf:"varint,2,opt,name=dueDate,proto3" json:"dueDate,omitempty"`
        CancelDate           int64    `protobuf:"varint,3,opt,name=cancelDate,proto3" json:"cancelDate,omitempty"`
        LastUpdate           int64    `protobuf:"varint,4,opt,name=lastUpdate,proto3" json:"lastUpdate,omitempty"`
        Amount               float32  `protobuf:"fixed32,5,opt,name=amount,proto3" json:"amount,omitempty"`
    }
    
    type BonusPayment struct {
        Id                   string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
        ProductId            int64    `protobuf:"varint,2,opt,name=productId,proto3" json:"productId,omitempty"`
        Amount               float32  `protobuf:"fixed32,3,opt,name=amount,proto3" json:"amount,omitempty"`
        CreateDate           int64    `protobuf:"varint,4,opt,name=createDate,proto3" json:"createDate,omitempty"`
        CancelDate           int64    `protobuf:"varint,5,opt,name=cancelDate,proto3" json:"cancelDate,omitempty"`
        LastUpdate           int64    `protobuf:"varint,6,opt,name=lastUpdate,proto3" json:"lastUpdate,omitempty"`
    }
    

    Give it a try and let me know if works.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c