我希望所有唯一的数组值都在mongodb的一个字段中

我希望所有唯一的数组值都在mongodb的一个字段中,mongodb,filter,distinct,unique-values,Mongodb,Filter,Distinct,Unique Values,我正在使用MongoDB。我在一个集合的文档中有多个字段,包括颜色、样式、关键字、形状等,所有数据类型都是一个数组。我希望所有的颜色,风格,形状,关键字等独特的价值观,从所有的文件 样本文件如下所示 "_id": "5de63ae0d88ea145cc7accb5", "id": "28011", "name": "BID 2", "slug": "bid-2", "description": "<p>Decorative details of graphic f

我正在使用MongoDB。我在一个集合的文档中有多个字段,包括颜色、样式、关键字、形状等,所有数据类型都是一个数组。我希望所有的颜色,风格,形状,关键字等独特的价值观,从所有的文件

样本文件如下所示

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

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

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

// your query is..

db.collection.distinct(color,style,shape)
可以使用和完成

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

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

用样本数据提问,并提供您尝试过的内容far@sathish请用示例文档和响应示例检查修改后的问题。我使用此查询多次获得相同的值。我需要颜色、形状等独特的数据。更不用说我将$concatArray更新为$setUnion,并为我完美工作。多谢各位@SathishIt的表现不好,超过5次没有记录。大约需要6秒钟。你能帮我提高查询性能吗?你能试着在你要查询的确切字段上创建索引吗?我已经在所有字段上添加了索引。但同样的表现。
// syntax is..
             db.collection.distinct(fields,query)

// your query is..

db.collection.distinct(color,style,shape)
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"
                     ]
                  }
               }
            }
         }
      }
   ]
);