{
'id' : 1,
'properties':{
'type': [[0.05, 'education'],[0.6, 'health'],[0.9, 'math']]
}
},
{
'id' : 1,
'properties':{
'type': [[0.05, 'education'],[0.01, 'health'],[0.9, 'math']]
}
},
如何根据数组的嵌套数组中的两个值参数进行查询?目标是返回具有属性的第一个文档。type[0]>0.5
和属性。type[1]==“health”
。
尝试了以下查询但没有成功。
db.collection.find({'properties.type': {'$elemMatch':{'$elemMatch':{'$all': [{ "$gt": 0.5, "$lt": 1 },'health']}}}})
这里请尝试一下,我使用$Unwind
和$ArrayElemat
。
db.collection.aggregate([
{
$unwind: "$properties.type"
},
{
$match: {
"$and": [
{
"$expr": {
"$gt": [
{
$arrayElemAt: [
"$properties.type",
0
]
},
0.5
]
}
},
{
"$expr": {
"$eq": [
{
$arrayElemAt: [
"$properties.type",
1
]
},
"health"
]
}
}
],
}
},
{
$limit: 1
}
])