0

In this case How do I keep documents in aggregation with $unwind aggregation can be ommited but what if you have a more complex case where you need aggregation framework? How can you keep these documents and still unwind those who have non-empty lists?

My case involves aggregation pipeline with multiple stages (match, unwind, project, match, group,..), i simply cannot fallback to plain "match" because those nested arrays need to be presented in a form of report (JasperReports). I first thought that this $unwind empty array will solve my problem but it won't because this array needs to be empty since I'm later grouping these values in JasperReport and showing them as they are.

I hope I was clear enough. Any ideas?

Community
  • 1
  • 1
sbuljat
  • 29
  • 4
  • The aggregation pipeline produces k documents when unwinding a length k array, including if k = 0. To keep the documents, you can stuff a dummy value into the array (or a projected array). It might be more fruitful to ask about your specific case, as the full context of your problem might make other options available. – wdberkeley Nov 20 '14 at 18:32
  • Questions like these seem to be too broad. Please narrow it down by posting sample documents and the expected output along with what you have tried, if you have. – BatScream Nov 20 '14 at 23:28

1 Answers1

0

Starting from MongoDb 3.2, $unwind operator supports preserveNullAndEmptyArrays:<boolean>.

So when preserveNullAndEmptyArrays:true, it will also include value which doesn't have any data or empty data.

for more info, visit - https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/#document-operand-with-options

Ujjwal Kumar Gupta
  • 2,308
  • 1
  • 21
  • 32