|
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"}
|
|