Ticket: User Preferences after updating preferences get user from DB returns null

Hi, I have a problem with testUpdateSinglePreferences() method. I’ve updated user preferences in user collection

and it passes the first Assertion, but when I getting this user from the database field preference in user object is null. Where I’m going wrong?

Method for get a user: http://prntscr.com/mp8khl

I am having the exact same issue here. Have you come up with a fix for this yet?

I think I got it. We are supposed to update the “user.preferences” field. Instead of “user.favourite_club” field. So the Bson should look like, set(“preferences”, new Document(“favourite_club”, “FC Porto”)) for this one.

1 Like

Yeah it helped me, but still when I run the test with multiple preferences it fails, because of hardcode in new Document(“favourite_club”, “FC Porto”). I tried to create foreach through Map and get key and value in variables to send it instead of hardcode, but in database collection UpdateMany it’s just replacing first object all the time. Any suggestions, what command use to insert many preferences?

Oh! Sorry if I confused you. I didn’t mean hardcoding the document.
So roughly, I wrote:

  1. Convert userPreferences map into a Document object:
    Set keys = userPreferences.keySet();
    Document updates = new Document();
    for (String key : keys) {
    updates.append(key, userPreferences.get(key));

  2. update the preferences field with the updates document prepared above:
    Bson query = new Document(“email”, email); // query bson
    UpdateOptions options = new UpdateOptions();
    options.upsert(true); // set upsert to true
    userCollection.updateOne(query, set(“preferences”, updates), options);

1 Like

Ok, but test with null in preferences still failing, it is normal? I get NullPointerException

You should implement a null check in the update method, and if userPreferences is null, throw the expected exception.

1 Like