In general FindOne fmt.print result { }. I need to output Value.
I'm using pretty much standard setup from documentation: https://docs.mongodb.com/ecosystem/drivers/go/
I have problems with creating query because most of examples are different; I've tried to follow this pattern: https://github.com/ruanbekker/code-examples/blob/master/mongodb/golang/examples.go
Reference object:
_id:5d1a8829cf5042c071458db6
name:" !hello"
Value:" World %c end"
Counter:0
Code examples:
type userModel struct {
Uname string
Url string
}
var result userModel
filter := bson.D{{"name", " !hello"}}
db := Client.Database("Nothing").Collection("databaseC")
db.FindOne(context.Background(), filter).Decode(&result)
fmt.Println(result)
fmt.Println(result.Url)
// Output { 0} // Output empty
type userModel struct {
Name string
Value string
Counter int
}
var result userModel
findOneOptions := options.FindOne()
findOneOptions.SetProjection(bson.D{{"name", "!new"}})
filter := bson.D{{}}
db := Client.Database("Nothing").Collection("databaseC")
db.FindOne(context.TODO(), filter, findOneOptions).Decode(&result)
fmt.Println(result)
// Output nothing
// different collection with simple struct
type userModel struct {
Uname string
Url string
}
var result userModel
filter := bson.D{{"name", "object"}}
db := Client.Database("Nothing").Collection("Video")
db.FindOne(context.Background(), filter).Decode(&result)
fmt.Println(result)
fmt.Println(result.Url)
// Output { } // Output empty
type userModel struct {
name string `bson:"name"`
Value string `bson:"Value"`
Counter int `bson:"Counter"`
}
var result userModel
filter := bson.M{"name": " !hello"}
db := Client.Database("Nothing").Collection("databaseC")
db.FindOne(context.Background(), filter).Decode(&result)
fmt.Println(result)
fmt.Println(result.Value)
Outputs an actual value. Thanks @icza