Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate method in MongoDB Compass?

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

like image 319
Andrea Cristiani Avatar asked Jun 08 '18 08:06

Andrea Cristiani


1 Answers

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.

like image 157
Santiago M. Quintero Avatar answered Oct 18 '22 17:10

Santiago M. Quintero