0

I'm currently trying to generate a query from webservice from mongo. I hve something like this

'query' :
{ 
'startDate':{ '$lte':'2016-04-05T00:00:00.000Z' },
'$or':
  [
    { 'endDate': { '$gte': '2016-04-05T00:00:00.000Z' } }, 
    { 'endDate': { '$exists': false }}
  ]
}

My question is, is it possitble to add another set of '$or' into this json?

If so how do I do it? (ie. how do i create another $or key inside query)

So it becomes something like this

'query' :
{ 
'startDate':{ '$lte':'2016-04-05T00:00:00.000Z' },
'$or':
  [
    { 'endDate': { '$gte': '2016-04-05T00:00:00.000Z' } }, 
    { 'endDate': { '$exists': false }}
  ]
'$or':
  [  { <field> : <value>, <field> : <value> } ]
}

Thanks and Regards, Kev

Kev84
  • 827
  • 3
  • 15
  • 26
  • How would the query look like as a logical expression? What I mean is what type of operator would combine both $or? Why do you need it – Prinzhorn Apr 05 '16 at 19:15
  • Possible duplicate of [MongoDB Nested OR/AND Where?](http://stackoverflow.com/questions/23118434/mongodb-nested-or-and-where) – Prinzhorn Apr 05 '16 at 19:16
  • it would look like this: startDate < '2016-04-05' AND (end date > '2016-04-05 or endDate is null) AND (field1=value1 or field2=value2) – Kev84 Apr 05 '16 at 21:14

1 Answers1

1

Add new $and operator with two $or:

{ 
  'startDate':{ '$lte':'2016-04-05T00:00:00.000Z' },
  '$and': [
     {
       $or:[
         { 'endDate': { '$gte': '2016-04-05T00:00:00.000Z' } }, 
         { 'endDate': { '$exists': false }}
       ]
    },
    {
      '$or': [
         { <field> : <value>, <field> : <value> }
       ]
    }
}
alexmac
  • 19,087
  • 7
  • 58
  • 69