D
D
Dmitrii Solovev2016-04-06 21:13:34
MongoDB
Dmitrii Solovev, 2016-04-06 21:13:34

How to make a query in mongoDB using aggregation framework?

Understanding mongo and stuck on aggregation framework.
There are 2 collections, cash accounts (accounts) and transactional (transactions).
Transactions store a link on which account they were made, or two links if it was a transfer from one to another.
Online preview accounts: www.jsoneditoronline.org/?id=0957b9a7234970747f586...
Online preview transactions: www.jsoneditoronline.org/?id=c71dd28067663766ecd43...
At the output, using the aggregation framework, I want to get an array of statistics on accounts of this type :

[{
  "name": "Cash",
  "_id": "570549ea97cf9917637a5243",
  "transactions": "количество транзакций по этому аккаунту",
  "spent": "сумма всех полей amount из транзакций с типом spent",
  "earned": "сумма всех полей amount из транзакций с типом earned",
  "withdrawal": "сумма всех полей amount из транзакций с типом transfer, где данный аккаунт является source",
  "deposits": "сумма всех полей amount из транзакций с типом transfer, где данный аккаунт является destination",
  "summary": "(earned + deposits) - (spent + withdrawal)"
}]

How to make a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-04-07
@lega

I would store all these fields in the account (account), see reliable transfer of money from account to account: https://docs.mongodb.org/manual/tutorial/perform-t...
No need to get out with a report, yes and all the necessary data can be quickly obtained by one "point" query.
And how are you, in your approach, going to protect yourself from double spending money in the negative?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question