[GODRIVER-1182] FindOne.Decode not decoding embedded struct fields Created: 02/Jul/19  Updated: 09/Jul/19  Resolved: 09/Jul/19

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

Type: Bug Priority: Major - P3
Reporter: prakash Assignee: Emmanuel Eppinger (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux-ubuntu



 Description   

I have this following structure in my collection

 

 

{
 "userID": "1234",
 "name": "Prakash",
 "email": "prakash@example.com",
 "street1": "some street 1",
 "street2" : "some street 2",
 "city" : "some city",
 "state": "some state",
 "country": "some country"          
}

I am using the following struct (Address struct embedded)

type User struct {
   UserID string `bson:"userID"`
   Name string `bson:"name"`
   Email string `bson:email"`
   Address
}
 
type Address struct {
   Street1 string `bson:"street1"`
   Street2 string `bson:"street2"`
   City string `bson:"city"`
   State string `bson:"state"`
   Country string `bson:"country"`
}

 

 

So, When I do

 

user := &User{}
collection.FindOne(ctx, bson.M{"userID": "1234"}).Decode(user)

I am getting the userID, Name and Email field but the address fields are blank.

 

 

And when I am using this user struct, everything is working fine and

I am getting all data from MongoDB as required.

 

type User struct {
    UserID string `bson:"userID"`
    Name string `bson:"name"`
    Email string `bson:email"`
    Street1 string `bson:"street1"`
    Street2 string `bson:"street2"`
    City string `bson:"city"`
    State string `bson:"state"`
    Country string `bson:"country"`
}

 

 



 Comments   
Comment by Daniel Mejia R [ 09/Jul/19 ]

Thanks Manny, so I'd need to use something like this,

 

type MyStuff struct {
   StuffName string `json:"alt_name" bson:"name"`
}
 
type MyThing struct {
   Name string `json:"name"  bson:"name"`
   MyStuff `bson:"inline"`
}

In order to have

 

{
  "name":"Hello, playground",
  "alt_name":"This is the stuff"
}

Thanks in advance

 

Comment by Emmanuel Eppinger (Inactive) [ 09/Jul/19 ]

Hi All,

 

 

You should be able to use the `bson:"inline"` struct tag to enable this behavior.

 

Thanks and let us know if you have any more questions

Emmanuel

 

Comment by Daniel Mejia R [ 03/Jul/19 ]

I've found that this behaviour could be related to the decoder used by BSON itself

That's all I can say. I'm not saying is an issue with mongo, I just added additional information that might be related with the behaviour you are reporting, but we must wait for the dev-team to take on the issue

Comment by prakash [ 03/Jul/19 ]

Hey @Daniel,

I am confused. The problem is with MongoDB or I have to fix something in my application.

Comment by Daniel Mejia R [ 02/Jul/19 ]

I've found that this behaviour could be related to the decoder used by BSON itself when dealing with embeded structs.

Take a look for instance at the behaviour of the following: 

https://play.golang.org/p/OBbfb_6jPjH

At the database you would expect to have

 

{
  "name":"Hello, playground",
  "alt_name":"This is the stuff"
}

But instead I'm having

{
  "name":"Hello, playground",
  "mystuff":{
    "alt_name":"This is the stuff"
  }
}

 

Which is counter-intuitive given the standard behaviour for embeded structs and json Decoding 

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