O
O
olezhenka2018-08-27 08:50:35
MongoDB
olezhenka, 2018-08-27 08:50:35

How to make such query in mongodb?

I need to look for either the create_date field, but if it's not there, then I'm looking for the create_time field:

Sources_Timings.find(
  {
    create_date: {
      $gte: req.body.date.starting,
      $lte: req.body.date.ending
    },
      create_time: {
        $gte: 0,
        $lte: 86400000
      }
  function(err, timings) {
                 .......
  }
)

I can of course write this easily with two queries, but this is a crutch and I think there is a way to search for fields optionally

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RidgeA, 2018-08-27
@RidgeA

you can do this https://docs.mongodb.com/manual/reference/operator... , of course, but it can be costly in terms of performance

F
forspamonly2, 2018-08-27
@forspamonly2

$or: {
  create_date: {...}, 
  $and: { 
    create_date: { $exists: false }, 
    create_time: {...} 
  } 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question