Ticket: User Preferences - updates doesn't work

Hi!

I can’t understand why the update doesn’t work. I thought that as it might be multiple map pairs I should combine them into one update and if the updated fields don’t exist - it will create them.

My code looks like this

      List<Bson> updates = userPreferences.entrySet()
          .stream()
          .map(entry -> Updates.set(entry.getKey(), entry.getValue()))
          .collect(Collectors.toList());
      usersCollection.updateOne(Filters.eq("email", email), Updates.combine(updates));

Update result returns

    AcknowledgedUpdateResult{matchedCount=1, modifiedCount=1, upsertedId=null}

But it doesn’t create the fields that must be updated but missing - the tests fail due to missing keys in the user object. What is the problem?

Hi @Artsiom_15755,

Have you tried using Updates.set method to set the updates for userPreferences ?

Kanika

Erm, what do you mean? You mean to use pure Updates.set instead of combine?

Yes. Give it a try and let me know.

Kanika

I got was what the problem.
I was trying to set non-existing fields, while there was a filed preferences which I should have set directly.