How to count the number of occurrences of a specific object id in an array field

I have a mongo DB table called activities that looks something like this:

Each activity has an array called sections, and each element of that array has an instance and a componentType.

I would like to be able to count up all of the activities where one of the elements in the sections array has a component type of “Embedded Instance” and a instance equal to an objectId that I would specify.

How can I build this mongo query in Retool?

Hi @jburk!

According to this reference, you should be able to use dot notation + $elemMatch to access these nested array elements within an object and then include this inside of a Retool mongo count query. It might look something like this:

May have messed up the syntax here, but hopefully this is enough to get you started!