Search code examples
mongodbaggregation-frameworkaccumulator

Aggregate sets into superset


Given the following data in MongoDB:

[ 
  { id: 1, stuff: ["A", "B"] },
  { id: 2, stuff: ["B", "C"] },
  ... (lots and lots of records)
]

Is it possible to get the union of all "stuff" sets? e.g. ["A","B","C"]

I've tried using $addToSet

aggregate([
  { $group: {
      _id: null, 
      allStuff: { $addToSet: "$stuff" }
    }
  }
])

but that creates a set of sets e.g. [ ["A", "B"], ["B", "C"] ]


Solution

  • Ok, after showing your attempt, here is what you can do:

    db.a.aggregate([
      { $unwind : "$stuff" },
      { $group : {
        _id: null,
        all : {$addToSet : "$stuff"}
      }}
    ])
    

    In the beginning it unwinds all the elements in the arrays that you have and then just tries to add them all to the set.

    db.a.insert({ id: 1, stuff: ["A", "B"] })
    db.a.insert({ id: 2, stuff: ["B", "C"] })
    db.a.insert({ id: 3, stuff: ["A", "B", "C", "D"] })
    

    Gives you: { "_id" : null, "all" : [ "D", "C", "B", "A" ] }