Mongodb site, https://docs.mongodb.org/ecosystem/use-cases/pre-aggregated-reports/, offers this example of a document for faster look up at daily, monthly level.
{
_id: "201010/site-1/apache_pb.gif",
metadata: {
date: ISODate("2000-10-00T00:00:00Z"),
site: "site-1",
page: "/apache_pb.gif" },
daily: {
"1": {"sessions": 300, "bounces": 10}
"2": {"sessions": 100, "bounces": 5},
"3": {"sessions": 10},
"4": {"sessions": 100, "bounces": 4},
... }
}
For example, to retrieve data for a specific day
db.stats.monthly.find_one({ },{ 'daily.1': 1 'metadata': 1})
The above schema works great for me as well for most use cases as documented, as it is essentially just lookups.
For the few cases that we might have custom date range is something I'm struggling at, so if the user searches for 1st Jan - 3nd Jan => I'm ideally expecting this result.
[{
_id: "201010/site-1/apache_pb.gif",
metadata: {
date: ISODate("2000-10-00T00:00:00Z"),
site: "site-1",
page: "/apache_pb.gif" },
result: {
"sessions": 410, "bounces": 15 }
}, {
}, {
_id: "201010/site-1/apache_new.gif",
metadata: {
date: ISODate("2000-05-00T00:00:00Z"),
site: "site-1",
page: "/apache_new.gif" },
result: {
"sessions": 310, "bounces": 8 }
}, {
}...
]
I understand that we need to do aggregation here, but totally confused if it is even possible to aggregation by giving a range for keys in the embedded object.
Will I have to restructure my schema for this to be possible? I really love the efficient lookups, and they serve 80-90% of our use case.