如何使用Spring Boot从mongoDB集合访问嵌套数组?

时间:2020-06-26 20:45:00

标签: java mongodb spring-boot nested

我正在写一个信用卡管理系统。这是项目的数据库(每个用户可以有很多卡,每张卡可以有很多交易)

数据库代码示例:

public class PolicyServiceTest {

    private final PolicyServiceImpl policyService;

    @BeforeEach
    initializeService() {
        this.policyService = new PolicyServiceImpl();
        // Do whatever kind of initialization the policy service requires
    }

    @Test
    public void shouldCalculatePremium() {
        ...
    }
}

在java spring boot上,我有三个模型 User Card Transaction

{
    "_id": 1,
    "name": "Jawad",
    "lastname": "Zakhour",
    "username": "jwdzkh",
    "password": "pass123",
    "cards": [{
        "cardnumber": "1234-5678-9123-4567",
        "cardholdername": "yolla kazan",
        "expirationdate": "05/09/2021",
        "cvv": "256",
        "type": "Credit",
        "creditlimit": "3500",
        "transactions": [{
            "date": "03/06/2020",
            "amount": 750,
            "receiptNo": "EAC-15-123-45678"
        }, {
            "date": "06/08/2020",
            "amount": 320,
            "receiptNo": "THY-18-568-5866"
        }]
    }, {
        "cardnumber": "4589-3256-7841-9655",
        "cardholdername": "nabil dagher",
        "expirationdate": "06/07/2022",
        "cvv": "365",
        "type": "Debit",
        "balance": "5200",
        "transactions": [{
            "date": "09/11/2019",
            "amount": 90,
            "receiptNo": "TYH-35-163-5896"
        }, {
            "date": "10/10/2020",
            "amount": 120,
            "receiptNo": "NJU-85-586-4287"
        }]
    }, {
        "cardnumber": "8976-3154-3187-3659",
        "cardholdername": "jawad zakhour",
        "expirationdate": "06/07/2022",
        "cvv": "365",
        "type": "Debit",
        "balance": "12000",
        "transactions": [{
            "date": "01/02/2018",
            "amount": 14,
            "receiptNo": "DFG-58-348-9863"
        }, {
            "date": "04/12/2019",
            "amount": 550,
            "receiptNo": "FGH-46-008-3478"
        }]
    }]
}

我还创建了两个存储库CardsRepository和UserRepository

如何退回特定用户的所有卡?

现在我想获取特定卡的所有交易,那怎么可能? 因为每张卡都有列表,所以应该可以。

1 个答案:

答案 0 :(得分:0)

要获取特定卡的交易记录,只需使用filter(<some_predicate>)。例如,

val user = userRepository.findById(id).orElseThrow(() -> UserNotFoundException(id));
val transactions = user.getCards()
        .stream()
        .filter(card -> card.getCardNumber().equals("1111.."))
        .findFirst()
        .map(Card::getTransactions)
        .orElseGet(Collections::emptyList);

在这里,如果与cardNumber等于谓词的谓词匹配的特定卡,它将被传递到流管道中的下一个操作,并且findFirst()返回该卡的首次出现。

根据请求进行编辑:

@GetMapping("/users/{username}/cards/{cardnumber}/transactions")    
public List<Transaction> getTransactionByCardnumber(@PathVariable String username, @PathVariable String cardnumber) {
    val user = userRepository.findById(id).orElseThrow(() -> UserNotFoundException(id));
    val transactions = user.getCards()
            .stream()
            .filter(card -> card.getCardnumber().equals(cardnumber))
            .findFirst()
            .map(Card::getTransactions)
            .orElseGet(Collections::emptyList);
}