In the application, we already have a mechanism of logging changes that happen in our entities.
For example, we have Category and Product entities,
where Category looks like:
Category {
name;
description
}
and Product looks like:
Product {
name;
price;
quantity
}
Currently, we log information when any of the fields of our entities were modified:
Assume, that for Category entity
the name property was changed twice;
the description property was changed 3 times;
and for a Product entity
the only price property was changed twice.
then in our LogCollection we will have the following Documents:
[
{
"_id": 1,
"entity": "Category",
"property": "name",
"changed": "2020-08-30"
},
{
"_id": 2,
"entity": "Category",
"property": "name",
"changed": "2020-08-31"
},
{
"_id": 3,
"entity": "Category",
"property": "description",
"changed": "2020-08-29"
},
{
"_id": 4,
"entity": "Category",
"property": "description",
"changed": "2020-08-30"
},
{
"_id": 5,
"entity": "Category",
"property": "description",
"changed": "2020-08-31"
},
{
"_id": 6,
"entity": "Product",
"property": "price",
"changed": "2020-08-20"
},
{
"_id": 7,
"entity": "Product",
"property": "price",
"changed": "2020-08-31"
}
]
The problem that I am trying to solve now:
I want to extract only the latest changes per each field for each unique entity.
I mean, that as result of some magic query I will get the following list of Documents:
[
{
"_id": 2,
"entity": "Category",
"property": "name",
"changed": "2020-08-31"
},
{
"_id": 5,
"entity": "Category",
"property": "description",
"changed": "2020-08-31"
},
{
"_id": 7,
"entity": "Product",
"property": "price",
"changed": "2020-08-31"
}
]
Those 3 Documents show me when exactly the latest changes for entity fields were made per each unique entity.
Could you please support me with a query to get those 3 Documents?
For sure, I have already reviewed Mongo DB documentation and found that potentially I have to use aggregation framework with $group and $max operators, but still I am stuck with writing real working query