1

I want to aggregate a document by unwinding a subdocument array and filter the results which has no element in the array. Is there a way to find the elements with 0 length subdocument array and exclude them in the aggregation?

Behlül
  • 3,412
  • 2
  • 29
  • 46
  • What defines an empty suboducment, also is it the field in the docuemnt you wanna make sure is empty or a field in the subdocument? – Sammaye Sep 23 '13 at 09:52
  • On the parent document, there is a field which is a subdocument array. I'm unwinding on that array. – Behlül Sep 23 '13 at 09:53

1 Answers1

1

Provided this is a field in the root document and not the subdocument you wish to check is empty or not you can do:

{$match:{'$subdocument':{$gt:{}}}},
{$unwind:'$subdocument'}

For reference, another method is stated here: Query for documents where array size is greater than 1 by @JohnnyHK

Community
  • 1
  • 1
Sammaye
  • 43,242
  • 7
  • 104
  • 146