[GODRIVER-1574] Decode at struct sub level won't working Created: 13/Apr/20  Updated: 15/Apr/20  Resolved: 14/Apr/20

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: 1.3.1, 1.3.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Thanakrit Nawasaykaew Assignee: Divjot Arora (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

At my struct have a sub level of document example

type (  A struct {    ID  primitive.ObjectID `bson:"_id,omitempty"`    Title string `bson:"title"  }  B struct {     ID primitive.ObjectID `bson:"_id,omitempty"`     Title string `bson:"title"  }  C struct {     A     Bs []B `bson:"b"`  })

when fetch data and decode data to struct C all of field return nil or empty
but if i setup struct have all of field such as 

type (
 C struct {
    ID primitive.ObjectID `bson:"_id,omitempty"` 
    Title string `bson:"title"
    Bs []struct{
       ID primitive.ObjectID `bson:"_id,omitempty"`
       Title string `bson:"title"
    } `bson:"b"`
 }
)

data return correctly 

how to fix this problem or this is bug?

thank you.



 Comments   
Comment by Thanakrit Nawasaykaew [ 15/Apr/20 ]

it's work thank you verymuch MR. Divjot Arora

Comment by Divjot Arora (Inactive) [ 14/Apr/20 ]

Hi thanakrit.nsk@gmail.com,

Thank you for the report. You can fix this by changing struct C to use the inline struct tag for field A:

type C struct {
    A `bson:",inline"`
    Bs []B `bson:"b"`}

You can find an explanation of the inline tag at https://pkg.go.dev/go.mongodb.org/mongo-driver/bson?tab=doc#hdr-Structs. I'm going to close out this ticket, but feel free to leave another comment or open a new ticket if you have further questions.

Generated at Thu Feb 08 08:36:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.