Error by creating view with aggregate and group

Hello,

I’m using MongoDB 5.0.0-rc3 and I would like to create a view for some statistic information of my collection. I have got the following aggregation statement

db.word.aggregate([
    {"$group": {
        "_id": "$pageid",
        "max": {"$max": "$confidence.mean"},
        "min": {"$min": "$confidence.mean"},
        "avg": {"$avg": "$confidence.mean"}
    }}
])

This works well and as expected and I try to convert this into a view with:

db.createView("wordstatistic", "word", [{
    "$aggregate": {
        "$group": {
            "_id": "$pageid",
            "max": {"$max": "$confidence.mean"},
            "min": {"$min": "$confidence.mean"},
            "avg": {"$avg": "$confidence.mean"}
        }
    }
}])

But I get an error at the $aggregate that this statement is not correct. How can I fix this?
Thanks a lot

Hi @Philipp_Kraus ,

Welcome to MongoDB community.

A view requires only the pipeline array as an input :

db.createView("wordstatistic", "word", [{
        "$group": {
            "_id": "$pageid",
            "max": {"$max": "$confidence.mean"},
            "min": {"$min": "$confidence.mean"},
            "avg": {"$avg": "$confidence.mean"}
        }
    }
])

Thanks
Pavel

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.