M
M
Mashush2022-04-01 20:58:31
JavaScript
Mashush, 2022-04-01 20:58:31

How to filter documents in mongoose when searching?

There is a DeviceModel which contains 3 fields - _id, userId, refreshToken

So for the JWT I provide the ability to bind a refreshToken to a specific device until the user has logged out. But if the user clears cookies in the browser, then he will need to log in again, which means he will create a new DeviceModel document, while the previous one will not be deleted.

One of the functions of the administrator is to clean up the DeviceModel, namely, to find and delete those documents where the refreshToken has already died. The special method validateToken is used to validate the token (it will return either the payload of the token or null)

Is it possible to write a request to the DeviceModel in such a way as to filter the data? As a filter object in the find method, specify somehow the method for checking the token

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Step, 2022-04-01
@Mashush

In any case, you will have to loop through the documents and check the token.
I recommend that you first understand how the validateToken function works, how it understands that the token is not rotten. Maybe only time matters there, it will help to understand what else can be written to the database, in order to filter it later on this basis.
In the collection, additionally write created_at. And by "krone" pull out tokens older than a day, for example, iterate in a loop, validate and delete.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question