Mongoose使用$和两个过滤器应用相同的属性



我有一个状态属性,我想应用日期过滤器只有当状态是待定,对于其他状态则不需要日期筛选器。

  1. 只返回添加到"$in"的状态。
  2. 如果状态为挂起,应用日期过滤器(日期小于当前时间)。

我确实喜欢这个,但是它不能正常工作。

filter = { $and: [ { 
'package' : {$in: ids}, 
'status':  {$in: ['pending', 'accepted',  'charged', 'captured']},
date  
},
{
$and:[
{status : 'pending', date:{$lt:current_time}},
]
}
]
};
  • 使用$or代替$and,因为它将满足
  • 其中一个条件
  • pending状态从$in状态中移除
  • $and包含package条件和$or条件
filter = {
$and: [
{ package: { $in: ids } },
{
$or: [
{ status: { $in: ["accepted", "charged", "captured"] } },
{
status: "pending",
date: { $lt: current_time }
}
]
}
]
};

游乐场

最新更新