Skip to content

Storing password gives Invalid UTF-8 string in BSON document (in mongodb compass) error #6

@disharjayanth

Description

@disharjayanth

code:
func main() {
users := map[string]string{
"admin": "fCRmh4Q2J7Rseqkz",
"packt": "RE4zfHB35VPtTkbT",
"mlabouardy": "L3nSFRcZzNQ67bcc",
}

ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
if err = client.Ping(context.TODO(), readpref.Primary()); err != nil {
	log.Fatal(err)
}

collection := client.Database(os.Getenv("MONGO_DATABASE")).Collection("users")
h := sha256.New()

for username, password := range users {
	collection.InsertOne(ctx, bson.M{
		"username": username,
		"password": string(h.Sum([]byte(password))),
	})
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions