Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge output from unwind mongodb?

For example, I have data in a collection like this:

db.inventory2.insertMany([
  { "_id" : 1, "item" : "ABC", price: NumberDecimal("80"), "sizes": [ "S", "M", "L"] },
  { "_id" : 2, "item" : "EFG", price: NumberDecimal("120"), "sizes" : [ ] },
  { "_id" : 3, "item" : "IJK", price: NumberDecimal("160"), "sizes": ["M"] },
])

And, when adding $unwind, I want to select the item which has sizes is not null.

The result after I add $unwind:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

db.inventory2.aggregate( [ { $unwind: { path: "$sizes" } } ] )
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : "S" }
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : "M" }
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : "L" }
{ "_id" : 3, "item" : "IJK", "price" : NumberDecimal("160"), "sizes" : "M" }

My question is, when I add $unwind, how to not separate the result item "ABC". My expectation result is like this:

{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : ["S", "M", "L"] }
{ "_id" : 3, "item" : "IJK", "price" : NumberDecimal("160"), "sizes" : ["M"] }

Thanks before for your help.

>Solution :

Based on what I understand from your question, you are trying to filter the document with sizes is not an empty array.

You don’t need $unwind stage but a $match stage.

db.collection.aggregate([
  {
    $match: {
      sizes: {
        $ne: []
      }
    }
  }
])

Sample Mongo Playground 1


Or

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $ne: [
          {
            "$size": "$sizes"
          },
          0
        ]
      }
    }
  }
])

Sample Mongo Playground 2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading