mongodb - Multiple $and with multiple $or in $match mongoose -
expected condition
if(pid==req.session.pid && ((status==0 && statusdate1>=somedate)||(status==1 && statusdate2>=somedate)|| (status==2 && statusdate3>=somedate)||(status==0 && statusdate1>=somedate))) i have tried writing below $match statement meet above condition, reason not expected.
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}}] }] } } could please point me in right direction?
update
tried below versions, still did not expecting.
attempt 1
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}}] }] } } attempt 2
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}}] }] } } attempt 3
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}}] } ] } }
syntax :- { $or: [ expression1, expression2, ... ] }
see if you:-
var match = { $match: { $and: [ { practiceid: req.session.p_id }, { $or:[{status : 0, statusdate1:{$gte:somedate}}, {status : 1, statusdate2:{$gte:somedate}}, {status : 2, statusdate3:{$gte:somedate}}, {status : 3, statusdate4:{$gte:somedate}}] }] } } added , separated condition inside $or . see $or
Comments
Post a Comment