7

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

Andrea Cristiani
  • 351
  • 2
  • 5
  • 13
  • 4
    There is no aggregation support in compass. Use the mongo shell as compass is just an "explorer". – Neil Lunn Jun 08 '18 at 08:06
  • 3
    If you do not like mongo shell, then take a look into https://robomongo.org/ . I'm using Robo 3T, I found that it's very useful for prototyping/experimentation. – Neodan Jun 08 '18 at 08:31
  • i'm using Studio 3T aswell, and it's really good, but i wanted to try Compass cause it provides more stats for queries (i'm doing a university research on mongoDB performance) – Andrea Cristiani Jun 08 '18 at 08:38
  • 1
    I'd suggest trying Compass 1.14 (currently beta) which includes an [Aggregation Pipeline Builder](https://docs.mongodb.com/compass/beta/aggregation-pipeline-builder/). – Stennie Jun 08 '18 at 09:25
  • @Stennie thanks! I'll check it! – Andrea Cristiani Jun 08 '18 at 09:43

1 Answers1

6

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.

Tim Coker
  • 6,484
  • 2
  • 31
  • 62
Santiago M. Quintero
  • 1,237
  • 15
  • 22
  • 1
    Why is this answer down voted? As Stennie commented above this is available in version 1.14. – Mathias Dec 01 '19 at 09:02
  • 14
    The Aggregation Pipeline Builder is ok, but I don't see how to really run it after I'm done building. I can't believe the only way is to copy it to the shell or create a view from it?! – maf-soft Dec 10 '19 at 20:03