我想将所有唯一数组值一起放在mongodb的一个字段中

时间:2019-12-03 06:58:35

标签: mongodb filter distinct unique-values

我正在使用MongoDB。并且我在一个集合的文档中有多个字段,分别是颜色,样式,关键字,形状等,所有数据类型都是一个数组。我想要所有文档中的颜色,样式,形状,关键字等所有唯一值。

在下面的pr中采样文档

  "_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"
  ]
  }
}

我尝试使用$ groupby。但这给了我不同数组中的唯一数组。我想要下面的结果

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

2 个答案:

答案 0 :(得分:0)

这是MongoPlay Around

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

                      ])

或您可以使用此 您可以使用Distinct仅从定义的字段获取唯一值。

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

// your query is..

db.collection.distinct(color,style,shape)

答案 1 :(得分:0)

可以使用addToSetreduce

该组将所有值合并到一个文档中。

reduce将数组数组转换为数组

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"
                     ]
                  }
               }
            }
         }
      }
   ]
);