我有以下要求。 在下面的數組元素中,我必須選擇並比較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