Search code examples
node.jsmongoosemongoose-schema

Set custom condition to find from Model using mongoose find()


I just want to set some conditions in Mongoose find() to query from my Routine model. Is it possible in any way? or How to set such kind of custom conditions?

const List = await Routine.find({
        $where:
            () => {
                moment(currentDate).isBetween(this.startDate, this.endDate, undefined, '[]') == true &&
                    customFunc(this) == true
            }
    })

Where the customFunc() returns a boolean value


Solution

  • Theoretically, you can do something like this. But that depends on what you store in startDate and endDate as far the date formatting is concerned

    let curDate = new Date()
    const List = await Routine.find({
                                     startDate:{"$lt":curDate},
                                     endDate:{"$gt":curDate}
                                    })