[GODRIVER-937] BSON Marshaller ignores fields of an embedded structure of the schema Created: 08/Apr/19  Updated: 09/Apr/19  Resolved: 09/Apr/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 1.0.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Alexey Nosov Assignee: Kristofer Brandow (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 18.04 amd64



 Description   

I declare schemas. For example:

type Embedded struct {
  Name string `bson:"name"`
}
 
type Main struct {
  Embedded
  OtherField int64 `bson:"otherField"`
}

When I try to load an instance of Main structure from the database, e.g.:

cursor, err := client.Database("my").Collection("myColl").Find(context.TODO(), bsonD{})
 
if err != nil {
 return err
}
 
for cursor.Next(context.TODO()) {
  var m Main
  if err := cursor.Decode(&m); err != nil {
    return err
  }
 
  fmt.Printf("name=\"%s\"\n", m.Name) // outputs `name=""`
}



 Comments   
Comment by Kristofer Brandow (Inactive) [ 09/Apr/19 ]

Glad I could help!

Comment by Alexey Nosov [ 09/Apr/19 ]

Thank you very much! It works perfectly.

Comment by Kristofer Brandow (Inactive) [ 08/Apr/19 ]

devoter, If you want to properly embed this struct, you'll need to use an inline tag, like so:

type Main struct {
 
    Embedded `bson:",inline"`
 
    OtherField int64 `bson:"otherField"`
 
}

--Kris

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