对数组$ GTE和$ LTE
我有模式
{ summaries:
[
{
type: 'temp',
min: 10,
max: 40
},
{
type: 'humidity',
min: 3,
max: 40
}
],
thresholds: [
{
type: 'temp',
low: -10,
high: 40
},
{
type: 'humidity',
low: 9,
high: 30
}
]
}
上面的这个对象是集合的一个记录。我想记录,如果summaries.type ==“临时”和thresholds.type ==“临时”和summaries.min> thresholds.low
摘要对象与阈值比较的对象必须是同一类型。温度与温度相比,哼哼比较嗡嗡声。有任何对象摘要温度那分钟<任何阈值对象温度将被返回
例:
{ summaries:
[
{
type: 'temp',
min: 10,
max: 40
},
{
type: 'humidity',
min: 3,
max: 40
}
],
thresholds: [
{
type: 'temp',
low: -10,
high: 40
},
{
type: 'humidity',
low: 9,
high: 30
}
]
}
将返回临时分钟= 10,阈值低= -10。
{ summaries:
[
{
type: 'temp',
min: 10,
max: 40
},
{
type: 'humidity',
min: 3,
max: 40
}
],
thresholds: [
{
type: 'temp',
low: 20,
high: 40
},
{
type: 'humidity',
low: 9,
high: 30
}
]
}
将因为被不返回临时分钟= 10,阈值温度低= 20尽管阈值低哼声= 9
回答如下:您可以使用$expr
使用下面的查询
db.collection.find({
"$expr": {
"$gt": [
{
"$arrayElemAt": [
"$summaries.min",
{ "$indexOfArray": ["$summaries.type", "temp"] }
]
},
{
"$arrayElemAt": [
"$thresholds.low",
{ "$indexOfArray": ["$thresholds.type", "temp"] }
]
}
]
}
})