How to get a single email using mongodb and go?

Am i doing right thing sir?
i have to query single email for user login so i develop this method
but this method won’t returning any error but can listing emails and it’s related details
can you help me to figure it out?

func Find_User(user *model.User) (*mongo.SingleResult, error) {
	//Create a handle to the respective collection in the database.
	//finding user data using email and password
	query := bson.M{"email": user.Email}
	collection := clientInstance.Database(AUTH_DB).Collection(USER_COLLECTION)
	return collection.FindOne(context.TODO(), query), clientInstanceError


You’re missing to decode into a struct. Also, I recommend to write in a more procedural way so it’s easier to see the steps of the operation:

func FindUser(user *model.User) (model.User, error) { 
    filter := bson.M{"email": user.Email}
	result := model.User{}
	err := clientInstance.Database(AUTH_DB).Collection(USER_COLLECTION).FindOne(context.Background(), filter).Decode(&result)
	if err != nil {
		if errors.Is(err, mongo.ErrNoDocuments) {
			return model.User{}, nil

		return model.User{}, fmt.Errorf("finding user: %w", err)

	return result, nil
1 Like

Thanks a lot sir.

if you can i need another help sir how can i sit password and email authentication and return useful reply using this same code , if i use json response it shows some error . if you please help me because i can’t able to declare them straight away there

That really depends on you authentication implementation. You could compare the password you have for the user against the one is trying to use for log in and respond accordingly, but I wouldn’t do it at the storage level.

1 Like

sir i have sended you the full code as you describe in your DM will you please help me sir? to fix the issue .

This post was flagged by the community and is temporarily hidden.