Aggregate method in MongoDB Compass?

Andrea Cristiani picture Andrea Cristiani · Jun 8, 2018 · Viewed 12.8k times · Source

as stated in the title i'm having some problems querying from MongoDB Compass using the aggregate methhod. I have a collection of documents in this form:

{"Array":[{"field":"val","field2":"val2"},{"field":"val","field2":"val2"},{"field":"val","field2":"val2"},{"field":"val","field2":"val2"},{"field":"val","field2":"val2"},...]}

using mongo shell or Studio 3T software I query it with aggregate method, follows an example:

db.collection.aggregate([
     { $match: {"Array.field": "val"}}, 
     { $unwind: "$Array"},
     { $match: {"Array.field": "val"}},
     { $group: {_id: null, count: {$sum:NumberInt(1)}, Array: {$push: "$Array"}}},
     { $project: {"N. Hits": "$count", Array:1}}
])

where I look for elements of Array who has field's value = "val" and count them. This works perfectly, but I don't know how to do the same in MongoDB Compass enter image description here

in the query bar I have 'filter', 'project' and 'sort' and I can do usual queries, but i don't know how to use aggregate method. Thanks

Answer

Santiago M. Quintero picture Santiago M. Quintero · Aug 10, 2019

You are looking at the Documents tab which is restricted for querying documents.

Take a look in the second tab called Aggregations where you can do your aggregation pipelines, as usual.

For further information please visit the Aggregation Pipeline Builder documentation.