I am trying the use of mongodb and Go and I cannot get the distinct values of the field in a collection.
This is my code:
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type House struct {
Ciudad string
}
func main() {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
collection := client.Database("test").Collection("houses")
var house repository.House
fmt.Println(collection.Distinct(ctx, "City", &house))
}
After execute this always I am geting an empty array. Any idea that is wrong in this code?