我有一個客戶微服務(客戶檔案)和一個銀行賬戶微服務(銀行賬戶檔案)。我如何構建這些需求?
有兩種型別的客戶:個人和企業。
個人客戶最多只能擁有一個儲蓄戶口、一個支票戶口或定期戶口。
商業客戶可能沒有一個儲蓄或定期賬戶,但可能有多個支票賬戶。
在客戶檔案中,我有一個標識銀行帳戶的 ID 屬性,并且可以滿足第一個要求,但是,第二個表明如果它是一種業務型別,您可以擁有多個支票帳戶。
uj5u.com熱心網友回復:
D b
db={
"clients": [
{
"_id": 1,
"type": "personal",
"name": "Tom",
"createAt": ISODate("2022-01-10T11:23:25.184Z")
},
{
"_id": 2,
"type": "business",
"name": "Apple",
"createAt": ISODate("2022-01-12T05:10:42.220Z")
}
],
"accounts": [
{
"_id": 1,
"client_id": 1,
"type": "saving",
"money": 12000
},
{
"_id": 2,
"client_id": 1,
"type": "checking",
"money": 8000
},
{
"_id": 3,
"client_id": 2,
"type": "checking",
"money": 6000
},
{
"_id": 4,
"client_id": 2,
"type": "checking",
"money": 7000
}
]
}
總計的
db.clients.aggregate([
{
"$lookup": {
"from": "accounts",
"localField": "_id",
"foreignField": "client_id",
"as": "account_docs"
}
}
])
mongoplayground
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410188.html
標籤:
