mongo find all golang code example
Example: golang mongo fetch doc
import "go.mongodb.org/mongo-driver/mongo"
host := fmt.Sprintf("mongodb://%s:%s@%s:%s",
"username",
"password",
"127.0.0.1",
"27017")
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(host))
if err != nil {
log.Fatal(err)
}
col := client.Database("database_name").Collection("collection_name")
result := col.FindOne(context.Background(), bson.M{"filter_key":"filter_value"})
if result.Err() == mongo.ErrNoDocuments {
}
var m map[string]interface{}
if err := result.Decode(&m); err != nil {
log.Fatal(err)
}