mongoose - Mongoose 使用 $and 应用相同属性的两个过滤器
问题描述
我有一个状态属性,我只想在状态为 pending时应用日期过滤器,对于其他状态,不需要日期过滤器。
- 仅返回添加到“$in”的状态。
- 如果状态为待处理,则应用日期过滤器(日期小于当前时间)。
我确实喜欢这个,但它不能正常工作。
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 }
}
]
}
]
};
推荐阅读
- c# - 分配新的 CancellationTokenSource() 是否有任何问题?对一个变量不止一次?
- git - 错误:远程解包失败:完全读取包头之前的 eof
- pandas - 如何将熊猫期间索引更改为小写?
- c# - C# XDocument - 获取 XML 文件中属性的值
- python - 如何使用 python/osx 在按键上退出循环?
- python - Tweepy:忽略以前的推文以改进优化
- r - 更改 lm() 中用冒号指定的交互项的对比
- callback - 如何知道我的消息已在 MQTT 上收到
- jquery - 如何在 ionic 3 中使用 shepherd 和 jquery?
- ruby-on-rails - 如何在 XML 中定义多次出现