mongodb中的$or查询

mongodb中的$or查询,mongodb,mongoose,Mongodb,Mongoose,我需要查找字段create_date,但如果未定义create_date,则查找字段create_time: Sources_Timings.find({ create_date: { $or: [ { $gte: req.body.date.starting, $lte: req.body.date.ending }, {

我需要查找字段create_date,但如果未定义create_date,则查找字段create_time:

Sources_Timings.find({
    create_date: {
        $or: [
            {
                $gte: req.body.date.starting,
                $lte: req.body.date.ending
            },
            {
                $exists: false
            }
        ]
    },
    create_time: {
        $or: [
            {
                $gte: 0,
                $lt: 86400000
            },
            {
                $exists: false
            }
        ]
    }
},
function(err, timings) {
  ...
})
我的代码不工作。

是一个顶级运算符,对两个或多个表达式的数组执行操作,并选择至少满足其中一个表达式的文档

所以你应该是这样的

Sources_Timings.find({
  "$or": [
    {
      "create_date": {
        "$gte": req.body.date.starting,
        "$lte": req.body.date.ending
      }
    },
    {
      "create_time": {
        "$gte": 0,
        "$lt": 86400000
      }
    }
  ]
})

它的作品!(谢谢)