User Preferences: preferences weren't saved correctly

I am doing below assignment using Java
Chapter 2: User-Facing Backend
Ticket: User Preferences

I am facing issue and not getting proper response. I am getting error message as User Preferences: preferences weren’t saved correctly. I java console i am not getting any error.

I have tried with multiple ways still no luck. Below is the code i am trying at my end. Could anybody help on this to resolve.

public boolean updateUserPreferences(String email, Map<String, ?> userPreferences) {
// TODO> Ticket: User Preferences - implement the method that allows for user
// preferences to
// be updated.
// TODO > Ticket: Handling Errors - make this method more robust by
// handling potential exceptions when updating an entry.

	try {
		Set keys = userPreferences.keySet();
		Document updates = new Document();
		for (Object key : keys) {
			updates.append((String) key, (String) userPreferences.get(key));
			Bson query = new Document("email", email); // query bson
			UpdateOptions options = new UpdateOptions();
			options.upsert(true); // set upsert to true
			String key1 = (String) key;
			String values = (String) userPreferences.get(key);
			System.out.println("key1:" + key1 + " values:" + values);
			Bson updateOperation = push("favorite_fruit" , "watermelon");
			// usersCollection.updateOne(query, userPreferences);
			 Bson filter = eq("email", email);
			UpdateResult updateResult = usersCollection.updateMany(filter, updateOperation, options);
			
			//favorite_fruit values:watermelon
			//key1:favorite_number values:42
			/*
			 * if(updateResult.getMatchedCount() == 1) { System.out.
			 * println("=> Updating the doc with {\"student_id\":10000}. Adding comment.");
			 * return true; }
			 */
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return true;
}

What are the errors or incorrect results you are getting?

I am pretty sure you want to update the preferences of a single user. This means updateMany is probably not the method you want to use.

As per your suggestion now i have changed to below code.

usersCollection.updateOne(
eq(“email”, email),
combine(set(“favorite_fruit”, “watermelon”), set(“favorite_number”, “42”)));

There is not any error in console. In UI error message is showing as User preferences were not not saved successfully.

Probably because you are using hard coded values such as favorite_fruit, watermelon and favorite_number rather than the parameter userPreferences.