0

I am using MongoDB. and I have multiple fields in documents of one collection, which are color, style, keywords, shape, etc. and all data type is an Array. I want all the unique values of color, style, shape, keywords, etc. from all the documents.

Sample document as pr below

  "_id": "5de63ae0d88ea145cc7accb5",
  "id": "28011",
  "name": "BID 2",
  "slug": "bid-2",
  "description": "<p>Decorative details of graphic foliage interpreted in the latest colours such as black, pink, green and turquoise. The soft texture of the ceramic envelops with warmth even the coldest dish.</p>\r\n\r\n<ul>\r\n\t<li>Dishwasher and microwave safe</li>",
  "price": "0.00",
  "keywords": [
    "Kitchen",
    "cook",
    "cooking",
    "kitchens",
    "pantry",
    "kitchen room",
    "room",
    "cookhouse",
    "cookery",
    "cuisine",
    "cook room",
    "food",
    "Crockery",
    "dishware",
    "dishes",
    "plates",
    "platters",
    "stoneware",
    "bone china",
    "porcelain",
    "earthenware",
    "ironstone",
    "plate",
    "dish",
    "small dish",
    "fruit dish",
    "fruit plate",
    "",
    "dessert plate"
  ],
  "Country": [
    "Italy"
  ],
  "Shape": [
    "Round"
  ],
  "Brand": [
    "Bitossi Home"
  ],
  "Materials": [
    "Ironstone"
  ],
  "Usage": [
    "Fruit Plate"
  ],
  "Color": [
    "Pink"
  ]
  }
}

I tried with $groupby. but it's giving me unique arrays in different arrays. I want result like below

{'color' : ['blue', 'red', 'green', 'white', 'gold'],
'shape' : ['round', 'square'],
'style' : [],
'keywords' : ['room', 'kitchen']}

2 Answers2

0

here is MongoPlay Around

db.collection.aggregate([ {
                            $unwind: "$Color"
                          },
                          {
                            $unwind: "$Shape"
                          },
                          {
                            $group: {
                            _id: null,
                                color: {
                                    $addToSet: "$Color"
                                },
                                Shape: {
                                    $addToSet: "$Shape"
                                }
                           }
                         },
                         {
                           $project: {
                               _id: 0
                         }

                      ])

OR You can use this You can use Distinct for only getting unique values from defined field.

// syntax is..
             db.collection.distinct(fields,query)

// your query is..

db.collection.distinct(color,style,shape)
Developer
  • 3,309
  • 2
  • 19
  • 23
0

Can be done using addToSet and reduce

The group brings all the values into a single document.

The reduce converts the array of arrays into array

db.dimen.aggregate( 
   [ 
      { 
         "$group":{ 
            "_id":0,
            Color:{ 
               $addToSet:'$Color'
            },
            Shape:{ 
               $addToSet:'$Shape'
            }
         }
      },
      { 
         $project:{ 
            "Color":{ 
               $reduce:{ 
                  input:"$Color",
                  initialValue:[ 

                  ],
                  in:{ 
                     $concatArrays:[ 
                        "$$value",
                        "$$this"
                     ]
                  }
               }
            },
            "Shape":{ 
               $reduce:{ 
                  input:"$Shape",
                  initialValue:[ 

                  ],
                  in:{ 
                     $concatArrays:[ 
                        "$$value",
                        "$$this"
                     ]
                  }
               }
            }
         }
      }
   ]
);
Sathish
  • 332
  • 4
  • 11