$or query in mongodb

I need find field create_date, but if create_date is not defined, then find field 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) {
  ...
})

My code don't working.

Upvotes: 1

Views: 50

Answers (1)

Ashh
Ashh

Reputation: 46481

$or is a top level operator and perform operation on an array of two or more expressions and selects the documents that satisfy at least one of the expressions.

So you query should be some thing like this

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

Upvotes: 2

Related Questions