如何使用官方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.

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.