가정하면 다음과 같은 데이터 구조체:
type Product struct {
ID primitive.ObjectID `bson:"_id"`
Title string `bson:"product"`
Description string `bson:"description"`
count int64 // <- this is not exported, so data won't be filled
}
에작만 수출 분야나 방법에서 액세스할 수 있어 패키지입니다.
용 구조체 필드 태그를 말 Mongodb 드에서 데이터 수집트의 일치하는 필드가 있습니다. 여기에 경기 mongodb bson
태그에서 필드의 컬렉션입니다.
func main() {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
client, err = mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017/"))
if err != nil {
log.Fatalf("can't connect to database: %v\n", err)
}
objID, _ := primitive.ObjectIDFromHex("619dd79acad38082f9ce16af")
db := client.Database("db")
col := db.Collection("products")
filter := bson.D{
{"_id", objID},
}
dest := &Project{}
err := col.FindOne(ctx, filter).Decode(dest)
if err != nil {
log.Fatalln(err)
}
방법 Decode
Unmarshals 찾을 데이터로 dest
.