Multiple $and with multiple $or in $match mongoose
<p><strong>Expected condition</strong></p>

<pre><code>if(pid==req.session.pid &amp;&amp; 
   ((status==0 &amp;&amp; StatusDate1&gt;=somedate)||(status==1 &amp;&amp; StatusDate2&gt;=somedate)||
    (status==2 &amp;&amp; StatusDate3&gt;=somedate)||(status==0 &amp;&amp; StatusDate1&gt;=somedate)))
</code></pre>

<p>I have tried writing below <code>$match</code> statement to meet the above condition, but for some reason it is not as expected.</p>

<pre><code>var match =  { $match: { 
    $and: [ 
        { practiceId: req.session.p_id },  
        {
            $or:[{status : 0, StatusDate1:{$gte:somedate}}] 
        }, 
        {           
            $or:[{status : 1, StatusDate2:{$gte:somedate}}] 
        },
        {           
            $or:[{status : 2, StatusDate3:{$gte:somedate}}] 
        },
        {           
            $or:[{status : 3, StatusDate4:{$gte:somedate}}] 
        }]
    }
}
</code></pre>

<p>Could anyone please point me in the right direction?</p>

<p><strong>Update</strong></p>

<p>Tried below versions, still did not get what I was expecting.</p>

<p><strong>Attempt 1</strong></p>

<pre><code>var match =  { $match: { 
    $and: [ 
        { practiceId: req.session.p_id }],
    $and:[  
        {
            $or:[{status : 0, StatusDate1:{$gte:somedate}}] 
        }, 
        {           
            $or:[{status : 1, StatusDate2:{$gte:somedate}}] 
        },
        {           
            $or:[{status : 2, StatusDate3:{$gte:somedate}}] 
        },
        {           
            $or:[{status : 3, StatusDate4:{$gte:somedate}}] 
        }]
    }
}
</code></pre>

<p><strong>Attempt 2</strong></p>

<pre><code>var match =  { $match: { 
    practiceId: req.session.p_id ,
    $and:[  
        {
            $or:[{status : 0, StatusDate1:{$gte:somedate}}] 
        }, 
        {           
            $or:[{status : 1, StatusDate2:{$gte:somedate}}] 
        },
        {           
            $or:[{status : 2, StatusDate3:{$gte:somedate}}] 
        },
        {           
            $or:[{status : 3, StatusDate4:{$gte:somedate}}] 
        }]
    }
}
</code></pre>

<p><strong>Attempt 3</strong></p>

<pre><code>var match =  { $match: { 
    practiceId: req.session.p_id,
        $and:[  
        {
            $or:[{status : 0,StatusDate1:{$gte:somedate}}], 
            $or:[{status : 1,StatusDate2:{$gte:somedate}}], 
            $or:[{status : 2,StatusDate3:{$gte:somedate}}], 
            $or:[{status : 3,StatusDate4:{$gte:somedate}}] 
        }
    ]
  }
</code></pre>

<p>}</p>