在这里输入代码查询解释下面的输出给我。我想要不同文档数组中的唯一值。当前查询在大约5个缺少文档的情况下运行,大约需要5秒钟以上的时间。所有组字段和活动字段也都设置为索引。我尝试使用$ unwind所有组字段,然后将$ group与$ addToSet一起使用,该方法也几乎花了相同的时间。任何人都可以帮助提高查询性能。
查询样本
db.products_v4.explain(
).aggregate( [
{
"$match":{
"active":"1"
}
},
{
"$group":{
"_id":0,
"Brand":{
"$addToSet":"$Brand"
},
"Country":{
"$addToSet":"$Country"
},
"Categories":{
"$addToSet":"$Categories"
},
"Feature":{
"$addToSet":"$Feature"
},
"Designers":{
"$addToSet":"$Designers"
},
"Style":{
"$addToSet":"$Style"
},
"Materials":{
"$addToSet":"$Materials"
},
"Base Material":{
"$addToSet":"$Base Material"
},
"Top material":{
"$addToSet":"$Top material"
},
"Upholstery Material":{
"$addToSet":"$Upholstery Material"
},
"Finish":{
"$addToSet":"$Finish"
},
"Seating Capacity":{
"$addToSet":"$Seating Capacity"
},
"Shape":{
"$addToSet":"$Shape"
},
"Subject":{
"$addToSet":"$Subject"
},
"Upholstery":{
"$addToSet":"$Upholstery"
},
"Material":{
"$addToSet":"$Material"
},
"Usage":{
"$addToSet":"$Usage"
},
"Colour Temperature":{
"$addToSet":"$Colour Temperature"
},
"Light Source":{
"$addToSet":"$Light Source"
}
}
},
{
"$project":{
"_id":0,
"Brand":{
"$reduce":{
"input":"$Brand",
"initialValue":[
[
]
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Country":{
"$reduce":{
"input":"$Country",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Categories":{
"$reduce":{
"input":"$Categories",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Feature":{
"$reduce":{
"input":"$Feature",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Designers":{
"$reduce":{
"input":"$Designers",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Style":{
"$reduce":{
"input":"$Style",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Materials":{
"$reduce":{
"input":"$Materials",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Base Material":{
"$reduce":{
"input":"$Base Material",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Top material":{
"$reduce":{
"input":"$Top material",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Upholstery Material":{
"$reduce":{
"input":"$Upholstery Material",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Finish":{
"$reduce":{
"input":"$Finish",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Seating Capacity":{
"$reduce":{
"input":"$Seating Capacity",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Shape":{
"$reduce":{
"input":"$Shape",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Subject":{
"$reduce":{
"input":"$Subject",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Upholstery":{
"$reduce":{
"input":"$Upholstery",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Material":{
"$reduce":{
"input":"$Material",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Usage":{
"$reduce":{
"input":"$Usage",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Colour Temperature":{
"$reduce":{
"input":"$Colour Temperature",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
},
"Light Source":{
"$reduce":{
"input":"$Light Source",
"initialValue":[
],
"in":{
"$setUnion":[
"$$value",
"$$this"
]
}
}
}
}
}
])
下面是文档样本数据
{
"_id":"5def3927c387114c4eb0733f",
"id":"47079",
"name":"LTY 17040065 1575958823PgY0T",
"slug":"lty-17040065",
"model_number":"lty 17040065",
"country_of_mfg":"China",
"description":"<ul>\r\n\t<li>Material: Solid Wood Frame + Glass+ Linen + Gold foil +MDF backboard</li>\r\n\t<li>Customization Available for Frame Material, Finish & Size</li>\r\n\t<li>Size : \r\n\t<ul>\r\n\t\t<li>L - 800 mm x W - 800 mm</li>\r\n\t\t<li>L - 1000 mm x W - 1000 mm</li>\r\n\t</ul>\r\n\t</li>\r\n</ul>",
"brand_id":"553",
"price":"0.00",
"primary_image_url":"https://dt4f7ywfipgvt.cloudfront.net/products_images/47079/optimized/lty_17040065-1.jpg",
"keywords":[
"Paintings",
"Painting",
"picture",
"painting",
"paint",
"paints",
"color",
"colors",
"colour",
"depiction",
"art",
"art work",
"decor",
"decoration",
"decorations",
"decorates",
"decorative",
"indoor",
"inside",
"in home",
"inside home",
"for home",
"for room",
"inside room",
"home",
"wooden frame painting",
"metal frame painting",
"printed painting",
"printed art picture",
"gold foil art",
"post modern art",
"postmodern art"
],
"new":"0",
"active":"1",
"featured":"0",
"featured_big_flag":"0",
"uom":"Each",
"created_datetime":"2019-09-03 09:21:34",
"category_ids":"14",
"category_processed":"1",
"update_date":"2019-09-12 17:07:01",
"Country":[
"China"
],
"Frame Material":[
"Wood"
],
"Brand":[
"Laitin Art"
],
"Subject":[
"Abstract"
],
"Color":[
"Black",
"Gold"
],
"brand_data":{
"id":"553",
"vendor_id":"520",
"vendors_brands_id":"0",
"brand_name":"Laitin Art",
"active":"1",
"hide_brand_name":"0",
"featured":"0",
"synced":"1",
"priority":"1"
},
"categories_data":[
{
"id":"9347958",
"top_cat_id":"4",
"name":"Paintings",
"breadcrumb":"Rugs & Decor",
"breadcrumb_full":"Rugs & Decor> Paintings",
"breadcrumb_html":"<li><a href='/product-list/rugs-decor/1'>Rugs & Decor</a> </li><li> <a href='/product-list/paintings/1'>Paintings</a></li>",
"partial_breadcrumb_html":"<li><a href='/product-list/rugs-decor/1'>Rugs & Decor</a> </li><li> Paintings</li>",
"slug":"paintings",
"type":"category",
"active":"1",
"products_count":"1195",
"category_id":"14",
"product_id":"47079"
},
{
"id":"9347959",
"name":"Rugs & Decor",
"breadcrumb":"Rugs & Decor",
"breadcrumb_full":"Decor> Decor",
"breadcrumb_html":"<li> Rugs & Decor</li>",
"partial_breadcrumb_html":"<li> Rugs & Decor</li>",
"slug":"rugs-decor",
"type":"category",
"active":"1",
"products_count":"8379",
"sub_cat_ids":"8,10,11,13,14,15,839,842,844,845,985,986,1742,1795,1851,1861,1867,1883,1909,1913,1930,1938,1958,1959",
"category_id":"4",
"product_id":"47079"
}
],
"Categories":[
"Paintings",
"Rugs & Decor"
],
"attributes":{
"Country":{
"values":[
"China"
],
"value_display":[
"China"
],
"active":"1",
"filterable":"1"
},
"Frame Material":{
"values":[
"Wood"
],
"value_display":[
"Wood"
],
"active":"1",
"filterable":"1"
},
"Brand":{
"values":[
"Laitin Art"
],
"value_display":[
"Laitin Art"
],
"active":"1",
"filterable":"1"
},
"Frame Type ":{
"values":[
"Framed"
],
"value_display":[
"Framed"
],
"active":"1",
"filterable":"1"
},
"Style":{
"values":[
"Post Modern"
],
"value_display":[
"Post Modern"
],
"active":"1",
"filterable":"1"
},
"Subject":{
"values":[
"Abstract"
],
"value_display":[
"Abstract"
],
"active":"1",
"filterable":"1"
},
"Color":{
"values":[
"Black",
"Gold"
],
"value_display":[
"Black",
"Gold"
],
"active":"1",
"filterable":"1"
}
},
"dummy":1
}
输出数据
{"Brand":["Amura","Arketipo","Ballabio Italia","Barel","Boiserie Italia","Casamilano","Cattelan Italia","Domus Arte","Francesco Pasi","Giellesse","Gyform","IL Loft","Medea Lifestyle","Morelato","Moroso","Oliver B","Olivieri","Rozzoni","Villusso",[]],"Country":["Italy"],"Categories":["Bed Benches","Beds","Double Beds","Furniture","Kids Beds","Queen Bed","Seating","Single Beds"],"Feature":["Casters","Detachable Guardrails","Footboard","Guardrails","Headboard","Headrest Adjustable","Height Adjustable","High Back","High Headboard","Integrated Nightstands","Medium Back","Open","Seat Upholstered","Storage","Tufted","Tufted Headboard","Upholstered","Upholstered Headboard"],"Designers":["Amuralab","Anton Cristell","CRS Medea","Casamilano Studio","Emanuel Gargano","Euro Sironi","Giuseppe Vigano","Libero Rutilo","Luca Scacchetti","Matteo Nunziati","Statilio Ubiali","Umberto Asnago","studio tecnico Gyform"],"Style":[],"Materials":["Ash","Engineered Wood","Fabric","Fiber","Foam","Gold Leaf","Iron","Leather","MDF","Metal","Plexi Glass","Polyester","Polyurethane Foam","Steel","Veneer","Wood"],"Base Material":["Crystal","Gold Leaf","Iron","Metal","Steel","Veneer","Wood"],"Top material":[],"Upholstery Material":["Canvas","Cotton","Fabric","Feather","Foam","Goose Feather","Half Leather","Hide","Leather","Nubuck Leather"],"Finish":["Glossy","Lacquered","Matt","Polished","Satin"],"Seating Capacity":[],"Shape":["Rectangular","Round","Square"],"Subject":[],"Upholstery":[],"Material":[],"Usage":[],"Colour Temperature":[],"Light Source":[]}
"stages" : [
{
"$cursor" : {
"query" : {
"active" : "1"
},
"fields" : {
"Base Material" : 1,
"Brand" : 1,
"Categories" : 1,
"Colour Temperature" : 1,
"Country" : 1,
"Designers" : 1,
"Feature" : 1,
"Finish" : 1,
"Light Source" : 1,
"Material" : 1,
"Materials" : 1,
"Seating Capacity" : 1,
"Shape" : 1,
"Style" : 1,
"Subject" : 1,
"Top material" : 1,
"Upholstery" : 1,
"Upholstery Material" : 1,
"Usage" : 1,
"_id" : 0
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "arcedior.products_v4",
"indexFilterSet" : false,
"parsedQuery" : {
"active" : {
"$eq" : "1"
}
},
"queryHash" : "20C0B395",
"planCacheKey" : "54690A78",
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"active" : 1
},
"indexName" : "active",
"isMultiKey" : false,
"multiKeyPaths" : {
"active" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"active" : [
"[\"1\", \"1\"]"
]
}
}
},
"rejectedPlans" : [ ]
}
}
},
{
"$group" : {
"_id" : {
"$const" : 0
},
"Brand" : {
"$addToSet" : "$Brand"
},
"Country" : {
"$addToSet" : "$Country"
},
"Categories" : {
"$addToSet" : "$Categories"
},
"Feature" : {
"$addToSet" : "$Feature"
},
"Designers" : {
"$addToSet" : "$Designers"
},
"Style" : {
"$addToSet" : "$Style"
},
"Materials" : {
"$addToSet" : "$Materials"
},
"Base Material" : {
"$addToSet" : "$Base Material"
},
"Top material" : {
"$addToSet" : "$Top material"
},
"Upholstery Material" : {
"$addToSet" : "$Upholstery Material"
},
"Finish" : {
"$addToSet" : "$Finish"
},
"Seating Capacity" : {
"$addToSet" : "$Seating Capacity"
},
"Shape" : {
"$addToSet" : "$Shape"
},
"Subject" : {
"$addToSet" : "$Subject"
},
"Upholstery" : {
"$addToSet" : "$Upholstery"
},
"Material" : {
"$addToSet" : "$Material"
},
"Usage" : {
"$addToSet" : "$Usage"
},
"Colour Temperature" : {
"$addToSet" : "$Colour Temperature"
},
"Light Source" : {
"$addToSet" : "$Light Source"
}
}
},
{
"$project" : {
"_id" : false,
"Brand" : {
"$reduce" : {
"input" : "$Brand",
"initialValue" : {
"$const" : [
[ ]
]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Country" : {
"$reduce" : {
"input" : "$Country",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Categories" : {
"$reduce" : {
"input" : "$Categories",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Feature" : {
"$reduce" : {
"input" : "$Feature",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Designers" : {
"$reduce" : {
"input" : "$Designers",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Style" : {
"$reduce" : {
"input" : "$Style",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Materials" : {
"$reduce" : {
"input" : "$Materials",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Base Material" : {
"$reduce" : {
"input" : "$Base Material",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Top material" : {
"$reduce" : {
"input" : "$Top material",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Upholstery Material" : {
"$reduce" : {
"input" : "$Upholstery Material",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Finish" : {
"$reduce" : {
"input" : "$Finish",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Seating Capacity" : {
"$reduce" : {
"input" : "$Seating Capacity",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Shape" : {
"$reduce" : {
"input" : "$Shape",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Subject" : {
"$reduce" : {
"input" : "$Subject",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Upholstery" : {
"$reduce" : {
"input" : "$Upholstery",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Material" : {
"$reduce" : {
"input" : "$Material",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Usage" : {
"$reduce" : {
"input" : "$Usage",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Colour Temperature" : {
"$reduce" : {
"input" : "$Colour Temperature",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
},
"Light Source" : {
"$reduce" : {
"input" : "$Light Source",
"initialValue" : {
"$const" : [ ]
},
"in" : {
"$setUnion" : [
"$$value",
"$$this"
]
}
}
}
}
}
],
"ok" : 1
}