golang mongo fetch doc
import "go.mongodb.org/mongo-driver/mongo"
// Inside a func
// Set up the client
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)
}
// Run the query
col := client.Database("database_name").Collection("collection_name")
result := col.FindOne(context.Background(), bson.M{"filter_key":"filter_value"})
if result.Err() == mongo.ErrNoDocuments {
// return nil
}
// Decode the value into a map or struct containing bson tags. e.g. type S struct { Name string `bson:"firstName"` }
var m map[string]interface{}
if err := result.Decode(&m); err != nil {
log.Fatal(err)
}
//return m
Are there any code examples left?
New code examples in category Go
-
Go 2022-03-27 22:30:23 golang check if ip is v6
-
Go 2022-03-27 16:20:18 make image go to center of page
-
Go 2022-03-24 15:20:01 why does my discord bot not work when i take code from googl
-
Go 2022-03-21 18:40:22 pass method as props vue
-
Go 2022-03-19 23:40:33 reverse a string in golang
-
Go 2022-03-18 12:45:06 golang while loop
-
Go 2022-03-17 19:30:08 instancing in godot
-
Go 2022-03-17 09:55:17 brew upgrae golang-migrate
-
Go 2022-03-17 06:15:06 Tushar Jadhav
-
Go 2022-03-17 00:00:12 golang cwd