My goal is to create a materialized view using an aggregation on an existing collection. I was following this presentation - Materialized Pre-Aggregations for Exploratory Analytics Queries. There’s an accompanying Github repo
I’ve tried these approaches:
- MongoDB Compass Aggregation Pipeline Builder
mongosh
shell
Both created a collection, which I verified via the Compass UI, the Atlas UI and the mongosh
shell. It’s visible when running the command db.getCollectionNames
and getCollectionInfos
and it’s visible in both Compass and Atlas.
Collection info from
mongosh
terminalRunning db.stats.findOne()
outputs this:
TypeError: db.stats.findOne is not a function
This is the pipeline
[
{
$addFields:
{
processed: {
$dateFromString: {
dateString: {
$replaceOne: {
input:
"$__dc_process.last_updated",
find: "@",
replacement: "",
},
},
},
},
},
},
{
$sort:
{
processed: -1,
},
},
{
$group:
{
_id: {
"campaign": "$_projectId",
"bot": "$bot",
"render-status": "$render-status",
"stream-status": "$stream.status",
"processor": "$__dc_process.satellite",
},
"total": {
$count: {},
},
"last_updated": {
$first: "$processed",
},
"last_updated_job": {
$first: "$$ROOT",
},
},
},
{
$out: "stats",
},
]
I’ve tried disconnecting and reconnecting the shell. In the UI for Compass and Atlas, querying does work and returns a result.
I’m not really sure if the fact that the group
stage is not first has any effect? The presenter notes that you should not do any filtering beforehand, but I haven’t done that here.
Any advice?