【问题标题】:I want all unique array value together in one field from mongodb我希望 mongodb 的一个字段中的所有唯一数组值一起
【发布时间】:2019-12-03 06:58:35
【问题描述】:

我正在使用 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']}

【问题讨论】:

  • 用示例数据提出问题,并提供您迄今为止尝试过的内容
  • @sathish 请使用示例文档和响应示例检查修改后的问题。

标签: mongodb filter distinct unique-values


【解决方案1】:

可以使用addToSet 和reduce 完成

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

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

【讨论】:

  • 我使用此查询多次获得相同的值。我想要颜色、形状等明智的独特数据。
  • 没关系,我将 $concatArray 更新为 $setUnion 并且非常适合我。谢谢你。 @Sathish
  • 它表现不佳,超过 5 条缺少记录。大约需要 6 秒。您能帮我提高查询性能吗?
  • 您可以尝试在您查询的确切字段上创建索引
  • 我已经为它们添加了索引。但性能相同。
【解决方案2】:

这里是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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    相关资源
    最近更新 更多