I'm writing client management web application. I'm trying to figure out the right way to manage my clients-payments relations. Once a day application is sending request to another API and synchronizing amount of payments for each client which I store in my database. I constantly need to run reports on payments (amount of payments) based on type of client (contract_type, sale_date and so on). I already have a clients
collection. I trying to choice between two schema:
{
"client_id": "asdf123",
"client_last_name": "BB",
"address": "123 Main St",
"city": "ATLANTA",
"payments_history": [
{
"contract_number": "asdf123",
"payment_date": ISODate("2012-09-02T07:00:00.0Z"),
"amount": 103.33,
"payment_number": NumberInt(1)
},
{
"contract_number": "asdf123",
"payment_date": ISODate("2012-09-30T07:00:00.0Z"),
"amount": 103.33,
"payment_number": NumberInt(2)
},
{
"contract_number": "asdf123",
"payment_date": ISODate("2012-11-04T07:00:00.0Z"),
"amount": 103.33,
"payment_number": NumberInt(3)
}
]
}
Versus creating separate collection "payments
", where each document is a payment
. I feel that it is better to separate those kind of data, since it will grow every single client
document to enormous amount of data with each query (which will still take a lot of memory if I'm choosing particular fields). But on the other hand I won't be able to run the aggregation reports ( since it based on data from two different collections). What is the best approach? Should I separate them and do aggregation with two different queries on server side (php)?