【发布时间】:2019-09-12 00:51:45
【问题描述】:
我用猫鼬,我有user集合的以下数据:
[{
"_id": "1",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
},
{
"value": "A70",
"text": "art"
}
]
},
{
"_id": "2",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
}
]
},
{
"_id": "3",
"notes": [
{
"value": "A80",
"text": "art"
}
]
}]
我有以下数组作为参数:[ "A90", "A80" ]
所以我想进行查询以使用此数组仅返回在 notes(值)表中包含所有数组项的记录。
因此,对于上面的示例,它将返回:
[{
"_id": "1",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
},
{
"value": "A70",
"text": "art"
}
]
},
{
"_id": "2",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
}
]
}]
我尝试了以下查找查询:
{ "notes": { $elemMatch: { value: { $in: valuesArray } } }}
但即使 valuesArray 中只有一个元素存在,它也会返回一条记录。
【问题讨论】:
-
你试过find吗?
-
我尝试了以下方法:
{ "notes": { $elemMatch: { value: { $in: valuesArray } } }}但即使valuesArray中仅存在一个元素,它也会返回一条记录,如果valuesArray中的所有元素都存在于@987654331 中,我想返回一条记录@
标签: mongodb mongoose mongodb-query