MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

Comment trouver par identifiant dans golang et mongodb

L'utilisation peut faire la même chose avec le pilote officiel Golang comme suit :

// convert id string to ObjectId
objectId, err := primitive.ObjectIDFromHex("5b9223c86486b341ea76910c")
if err != nil{
    log.Println("Invalid id")
}

// find
result:= client.Database(database).Collection("user").FindOne(context.Background(), bson.M{"_id": objectId})
user := model.User{}
result.Decode(user)