User Preferences ticket problem

https://university.mongodb.com/mercury/M220J/2021_April_6/chapter/Chapter_2_User-Facing_Backend/lesson/5ba53e42ef6c6a229da14a9f/problem

Hey everyone! So here i am dealing with this issue at 4am and cant figure out wth is going on.
Method is as simple as that:

     public boolean createUserSession(String userId, String jwt) {
        Document queryFilter = new Document("user_id", userId);
        Bson updateBson = new Document("user_id", userId).append("jwt", jwt);
        sessionsCollection.updateOne(queryFilter, updateBson, new UpdateOptions().upsert(true));
        return true;
    }

And i still get this

java.lang.IllegalArgumentException: Invalid BSON field name user_id

Stacktrace: https://pastebin.com/3jeiqFae

Anyone had been dealing with that and came with a solution by any chance?

Hi @Jaroslaw_Pierchala, welcome to the community,
you may use Filters.eq in the queryFilter.
check

Hi there! Alright to make it clear. I tried lots of diffirent things. Updates.set, “$set”, Filters aswell as aggregation framework. Same results for every scenario i came up with.

Alright for anyone who would encount the same problem. I have managed to fix it using combine()

public boolean createUserSession(String userId, String jwt) {
        //TODO> Ticket: User Management - implement the method that allows session information to be
        // stored in it's designated collection.
        Bson filter = Filters.eq("user_id", userId);
        sessionsCollection.updateOne(filter, combine(set("user_id", userId), set("jwt", jwt)), new UpdateOptions().upsert(true));
        return true;
        //TODO > Ticket: Handling Errors - implement a safeguard against
        // creating a session with the same jwt token.
    }
1 Like

you don’t have to update “user_id”. thus, you don’t have to update multiple fields, i.e., you don’t have to use “combine”

Well im using upsert, so basicly if i wouldnt combine it there would be a session without user_id which would be unrecognizable

I don’t have the documentation to back my claim. but at least in practice, mongodb can get the “user_id” value from the query parameter.
demo:
https://mongoplayground.net/p/T8CQbe-RhiY