我有以下要求。 在下面的数组元素中,我必须选择并比较LoanAmount的值。在之前的文章中,提到了下面的解决方案。Cloudant Selector查询获取阵列中的特定元素
{
"_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 记录的所有记录,请建议如何只获取特定元素在一个数组块内。
对于每个返回的记录,您确定没有subloans元素包含3000的loadamout? $ elemMatch不会返回匹配的数组,它返回整个文档。 –
一个非常精确的dupe:https://stackoverflow.com/questions/33262573/cloudant-selector-query – xpqz
类别。在这种情况下,OP特别要求仅返回数组中的特定元素。另一篇文章也提到了这个问题,但没有真正回答。 – markwatsonatx