【问题标题】:Set custom condition to find from Model using mongoose find()使用 mongoose find() 设置自定义条件以从模型中查找
【发布时间】:2020-07-31 09:38:41
【问题描述】:

我只想在 Mongoose find() 中设置一些条件来从我的 Routine 模型中查询。有没有可能?或者如何设置这种自定义条件?

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

customFunc() 返回一个布尔值

【问题讨论】:

  • 请假设 async/await 语法维护得当
  • 你可以使用$lt$gt,我假设startDate和endDate是模式字段?
  • 抱歉没有提及。是的 'startDate' 和 'endDate' 是架构字段@Karl L
  • 但我没有直接与任何模式字段进行比较,因为“currentDate”不是模式字段。那么如何完成呢? @Karl L

标签: node.js mongoose mongoose-schema


【解决方案1】:

理论上,你可以做这样的事情。但这取决于您在 startDateendDate 中存储的日期格式

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    相关资源
    最近更新 更多