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?

2
  • 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. Commented Nov 20, 2014 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. Commented Nov 20, 2014 at 23:28

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.