Ticket: User Preferences

Hi! Cannot get a pass on this ticket :expressionless: but i pass the unit test TestUpdateUserPreferences…
Plz help!

Here is the code :

public async Task<UserResponse> SetUserPreferencesAsync(string email,
            Dictionary<string, string> preferences, CancellationToken cancellationToken = default)
        {
            try
            {
                /**
                  Ticket: User Preferences
            
                  Update the "preferences" field in the corresponding user's document to
                  reflect the new information in preferences.
                */

                UpdateResult updateResult = null;
                // TODO Ticket: User Preferences
                // Use the data in "preferences" to update the user's preferences.
                //
                updateResult = await _usersCollection.UpdateOneAsync(
                    new BsonDocument("email", email),
                    Builders<User>.Update.Set("preferences", preferences),
                    new UpdateOptions { IsUpsert = false},
                    cancellationToken);

                return updateResult.MatchedCount == 0
                    ? new UserResponse(false, "No user found with that email")
                    : new UserResponse(true, updateResult.IsAcknowledged.ToString());
            }
            catch (Exception e)
            {
                return new UserResponse(false, e.Message);
            }
        }

Hi @Gustav_Naslund, welcome to the community.
Can you please share the error that is getting logged in the terminal when you are updating the user’s preference?

Also, can you please try again by replacing the arguments

with a callback function as below?

Builders<User>.Update.Set(u => u.Preferences, preferences)

If you have any doubts, please feel free to reach out to us.

Thanks & Regards,
Sourabh Bagrecha,
MongoDB