S
S
Sergey Popov2018-04-22 20:39:02
MongoDB
Sergey Popov, 2018-04-22 20:39:02

How to include condition in mongoose search?

Good evening.
There is a problem. I have a request to a bot. Depending on the presence of the flag in this query, I must enable search restrictions or disable it.
Conventionally, I have a list of users that have a registrationDate, name and activated field.
I want to extract regardless of the registration date in case if there is no flag (boolean type flag) and if there is, then only after a certain date.
There is a method.

//DB.getUsers - алиас для функции find() со стандартными параметрами.
let result = await db.getUsers({
    activated: true,

    // Здесь
    flag ? (registrationDate: {$gt: Date.now()}) : null
}, selectOptions)

Yes, you can do it with if-else, but I think there must be a way to solve such problems thought out in advance?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
levchak0910, 2018-04-22
@be_a_dancer

// Здесь
registrationDate: {$gt: flag ? someDate : 0}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question