[GODRIVER-847] Cannot retrieve/decode value form camelcase fields Created: 25/Feb/19  Updated: 27/Oct/23  Resolved: 25/Feb/19

Status: Closed
Project: Go Driver
Component/s: CRUD, JSON & ExtJSON, mgobson
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: James Jucutan Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Using struct below:

 

type User struct {
	Username  string    `json: "username" bson: "username"`
	FirstName string    `json: "firstName" bson: "firstName"`
	LastName  string    `json: "lastName" bson: "lastName"`
	Email     string    `json: "email" bson: "email"`
	Gender    string    `json: "gender" bson: "gender"`
	Password  string    `json: "password" bson: "password"`
	Enabled   bool      `json: "enabled" bson: "enabled"`
	BirthDate time.Time `json: "birthDate" bson: "birthDate"`
	CreatedAt time.Time `json: "createdAt" bson: "createdAt"`
	UpdatedAt time.Time `json: "updatedAt" bson: "updatedAt"`
	collection *mongo.Collection
}

When data is retrieved using:

 

 

func (u *User) FindByUsername(userName string) error {
	filter := bson.M{"username": userName}
	err := u.collection.FindOne(ctx, filter).Decode(&u)
	return err
}

All fields are populated except the ones with camelcase like 'firstName' and 'lastName'

 

Data retrieved from querying from console:

 

> db.users.find().pretty()
{
	"_id" : ObjectId("5c734c1a32bc9e3842b4deb1"),
	"username" : "jljucutan",
	"firstName" : "James",
	"lastName" : "Jucutan",
	"email" : "jljucutan@gmail.com",
	"password" : "",
	"enabled" : true,
	"gender" : "Male",
	"birthDate" : {
		"type" : {
			"code" : "function Date() {\n    [native code]\n}"
		}
	},
	"createdAt" : {
		"type" : {
			"code" : "function Date() {\n    [native code]\n}"
		},
		"default" : {
			"code" : "function now() {\n    [native code]\n}"
		}
	},
	"updatedAt" : {
		"type" : {
			"code" : "function Date() {\n    [native code]\n}"
		},
		"default" : {
			"code" : "function now() {\n    [native code]\n}"
		}
	}
}

Data obtained from calling from the code

 

{"Username":"jljucutan","FirstName":"","LastName":"","Email":"jljucutan@gmail.com","Gender":"Male","Password":"","Enabled":true,"BirthDate":"0001-01-01T00:00:00Z","CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01T00:00:00Z"}

 

 

 

 

 

 

 

 

 

 

 



 Comments   
Comment by James Jucutan [ 25/Feb/19 ]

I removed the space after 'json:' and 'bson:' and it's working now. Please close this case. Thanks!

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