Using Aggregation in Compass

I am using Compass and I am in the aggregation section. I am using the dropbox where I am using $match. I insert my query for example:

{
  instantClone: true, status: "PASS",
  leaseStart:{$gte : ISODate("2020-10-9T07:00:00Z"), $lt: ISODate("2020-10-10T07:00:00Z")}
}

I am trying to use the .count() or .countDocuments() at the end of the curly braces. It says "Stage must be a properly formatted document. How can I get the total number of documents if the sample only shows 20 max.

Hello @Jessica_Rios welcome to the Community!

When you use the aggregation builder than you need to add the stages individually. So first a $match than you will see the results on the right side. After this pls click “add stage” and add a $count stage.
That’s it :slight_smile:

Cheers
Michael

So I have one stage where the $match is and the data is displayed. Shouldn’t I save the data somewhere so I can do the $count? For the $count it’s asking for a string and I don’t know what to insert.

Hello @Jessica_Rios

it will look somewhat like shown in the screenshot. You should see the results of every stage on the right side when the stage definition fits

The string with the $count is just the filed name. Here is the link to the documentation were I too the screenshot: $count

You can add a https://docs.mongodb.com/manual/reference/operator/aggregation/out/ stage at the end. The string will be a collection name in which the results are written. In case the collection exists thee existing one is overwritten! To save the data to the collection named in $out you need to click the green button “save documents” which you find further right of the $out stage, you may need to scroll to the right.

you do not need to, the result of one stage is passed to the next stage on which a further stage can act, you can look on it as a filter which narrows down with every stage.

Hope this helps,
Michael

2 Likes

It’s more of a saved variable. Ok I read more about it and found information. Thank you.

As further resource I can reommed the free MongoDB class: M121: The MongoDB Aggregation Framework

Michael

2 Likes