【发布时间】:2017-08-02 12:27:06
【问题描述】:
我有以下要求。 在下面的数组元素中,我必须选择并比较 LoanAmount 的值。在之前的帖子中,提到了以下解决方案。
{
"_id": "65c5e4c917781f7365f4d814f6e1665f",
"_rev": "2-73615006996721fef9507c2d1dacd184",
"userprofile": {
"name": "tom",
"age": 30,
"employer": "Microsoft"
},
"loansBorrowed": [{"loanamount": 5000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 500,
"reasonforPenalty": "Exceeded the date by 10 days"
}
},
{
"loanamount": 3000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 400,
"reasonforPenalty": "Exceeded the date by 10 days"
}
},
{
"loanamount": 2000,
"loandate": "01/01/2001",
"repaymentdate": "01/01/2001",
"rateofinterest": 5.6,
"activeStatus": true,
"penalty": {
"penalty-amount": 500,
"reasonforPenalty": "Exceeded the date by 10 days"
}
}
]
}
Index:
{
"index": {
"fields": [{
"name": "loansBorrowed.[].loanamount",
"type":"number"
}],
"type": "json"
}
选择器查询:
{"selector": {
"loansBorrowed": {
"$elemMatch": {
"loanamount": 3000
}
}
}
}
但是索引和选择器查询提供了该特定查询的所有记录,而不是只为我提供 3000 条记录。 请建议如何仅获取数组块中的特定元素。
【问题讨论】:
-
对于返回的每条记录,您确定没有包含 3000 的 loadamout 的 subloans 元素吗? $elemMatch 不会返回匹配的数组,而是返回整个文档。
-
有点。在这种情况下,OP 专门询问是否只返回数组中的特定元素。另一个帖子中也有人问过,但没有真正回答。