Reputation: 2499
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.
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?
Upvotes: 1
Views: 33
Reputation: 2679
Your query should be like this:
.findOne({"session.user.uid": uid});
Because, uid
is in session.user
! Hope it helps.
Upvotes: 1