Hello everyone, I’ve 3 collections and i want to show all of them and for that i use lookup.
And after that i want to use divide method on one of the collection to divide some fields by some value(i.e. 5) but it returns an error that $divide can’t be used on array, so i used unwind and apply the divide method and it was working perfectly but the issue is that i want to merge them back in one array. Followed are the collections and query and required output.
---------- c_accounts --------------
// 01
{
"accountId" : "12345",
"customerId" : "1234",
"accountNumber" : "AC12345",
"balance" : 3242.2,
"balanceAed" : 32423.23
},
// 02
{
"accountId" : "12346",
"customerId" : "1234",
"accountNumber" : "AC12346",
"balance" : 12131,
"balanceAed" : 123.1
}
---------c_transactions----------
// 01
{
"customerId" : "1234",
"accountId" : "12345",
"transactionId" : "T12345",
"referenceNumber" : "R12345"
},
// 02
{
"customerId" : "1234",
"accountId" : "12346",
"transactionId" : "T12346",
"referenceNumber" : "R12346"
}
-------------c_cards---------------
// 01
{
"customerId" : "1234",
"accountId" : "12345",
"cardId" : "C1234",
"cardHolderName" : "John Doe",
"LimitAmount" : 15000.5,
"PaymentAmount" : 5000.5
},
// 02
{
"customerId" : "1234",
"accountId" : "12346",
"cardId" : "C1236",
"cardHolderName" : "John Doe",
"LimitAmount" : 15000.5,
"PaymentAmount" : 5000.5
}
----------------------- Query -------------------
db.getCollection("accounts").aggregate(
[
{
"$match" : {
"customerId" : "1234"
}
},
{
"$lookup" : {
"from" : "transactions",
"localField" : "accountId",
"foreignField" : "accountId",
"as" : "Transactions"
}
},
{
"$lookup" : {
"from" : "cards",
"localField" : "accountId",
"foreignField" : "accountId",
"as" : "Cards"
}
},
{
"$unwind" : {
"path" : "$Cards"
}
},
{
"$project" : {
"_id" : 0.0,
"Transactions._id" : 0.0,
"Cards._id" : 0.0
}
},
{
"$project" : {
// C-Accounts
"accountId" : 1.0,
"customerId" : 1.0,
"accountNumber" : 1.0,
"balance" : 1.0,
"balanceAed" : 1.0,
// C-Transactions
"Transactions.accountId" : 1.0,
"Transactions.customerId" : 1.0,
"Transactions.transactionId" : 1.0,
"Transactions.referenceNumber" : 1.0,
// C-Cards
"Cards.customerId" : 1.0,
"Cards.accountId" : 1.0,
"Cards.cardId" : 1.0,
"Cards.cardHolderName" : 1.0,
"Cards.LimitAmount" : {
"$divide" : [
"$Cards.LimitAmount",
5.0
]
},
"Cards.PaymentAmount" : {
"$divide" : [
"$Cards.PaymentAmount",
5.0
]
}
}
}
],
{
"allowDiskUse" : false
}
);
-------- Expected Output ----------
[
{
"accountId" : "12345",
"customerId" : "1234",
"accountNumber" : "AC12345",
"balance" : 3242.2,
"balanceAed" : 32423.23
},{
"accountId" : "12346",
"customerId" : "1234",
"accountNumber" : "AC12346",
"balance" : 12131,
"balanceAed" : 123.1
},
Transactions:[
{
"customerId" : "1234",
"accountId" : "12345",
"transactionId" : "T12345",
"referenceNumber" : "R12345"
},
{
"customerId" : "1234",
"accountId" : "12346",
"transactionId" : "T12346",
"referenceNumber" : "R12346"
}
],
Cards: [{
"customerId" : "1234",
"accountId" : "12345",
"cardId" : "C1234",
"cardHolderName" : "John Doe",
"LimitAmount" : 5000.5,
"PaymentAmount" : 1000.5
},
{
"customerId" : "1234",
"accountId" : "12346",
"cardId" : "C1236",
"cardHolderName" : "John Doe",
"LimitAmount" : 3000.5,
"PaymentAmount" : 1000.5
}
]
]