1

In short: I setup up session authentication for my node-express app with mongodb. If a user has already a valid session entity in the db a new session should NOT be created. Now I want to make a query for the specific user-id in my session collection.

Collection

The collection looks like follows:

{"_id":"HbAE-qMXPWl7C8tDUnC****q8OoEf76vN","expires":{"$date":{"$numberLong":"1576133617190"}},"session":"{\"cookie\":{\"originalMaxAge\":86399999,\"expires\":\"2019-12-12T06:36:29.898Z\",\"secure\":false,\"httpOnly\":true,\"path\":\"/\",\"sameSite\":true},\"user\":{\"uid\":\"5df08a1b8a60c174840d2986\"}}"}

And my query looks like this:

public async compareSessionIds(uid: string, sid: string) {
    //uid is: 5df08a1b8a60c174840d2986 ==> the same as in the entity
    const user: User | null = await this.connectionManager.db('users').collection<User>('sessions').findOne({uid: uid} as any);
    console.log('user found: ', user); // user found:  null
    return user && user._id === sid;
}

But my query returns everytime null so I am assuming my query is wrong. How do I query correctly for a specific nested field in mongodb?

1 Answer 1

1

Your query should be like this:

.findOne({"session.user.uid": uid});

Because, uid is in session.user! Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.