Các giai đoạn tổng hợp liên quan đến khía cạnh phân loại và nhóm đến các tài liệu đến. Chỉ định bất kỳ giai đoạn liên quan đến khía cạnh nào sau đây trong
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
2 của $facet
khác nhau để thực hiện tập hợp nhiều mặt:$facet
sub-pipeline's { "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
2 to perform a multi-faceted aggregation:
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
3{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
4{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
5
Các giai đoạn tổng hợp khác cũng có thể được sử dụng với $facet
với các ngoại lệ sau:
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
7$facet
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
9db.artwork.aggregate[ [ |
{ |
$facet: { |
"categorizedByTags": [ |
{ $unwind: "$tags" }, |
{ $sortByCount: "$tags" } |
], |
"categorizedByPrice": [ |
// Filter out documents without a price e.g., _id: 7 |
{ $match: { price: { $exists: 1 } } }, |
{ |
$bucket: { |
groupBy: "$price", |
boundaries: [ 0, 150, 200, 300, 400 ], |
default: "Other", |
output: { |
"count": { $sum: 1 }, |
"titles": { $push: "$title" } |
} |
} |
} |
], |
"categorizedByYears[Auto]": [ |
{ |
$bucketAuto: { |
groupBy: "$year", |
buckets: 4 |
} |
} |
] |
} |
} |
]] |
0db.artwork.aggregate[ [ |
{ |
$facet: { |
"categorizedByTags": [ |
{ $unwind: "$tags" }, |
{ $sortByCount: "$tags" } |
], |
"categorizedByPrice": [ |
// Filter out documents without a price e.g., _id: 7 |
{ $match: { price: { $exists: 1 } } }, |
{ |
$bucket: { |
groupBy: "$price", |
boundaries: [ 0, 150, 200, 300, 400 ], |
default: "Other", |
output: { |
"count": { $sum: 1 }, |
"titles": { $push: "$title" } |
} |
} |
} |
], |
"categorizedByYears[Auto]": [ |
{ |
$bucketAuto: { |
groupBy: "$year", |
buckets: 4 |
} |
} |
] |
} |
} |
]] |
1db.artwork.aggregate[ [ |
{ |
$facet: { |
"categorizedByTags": [ |
{ $unwind: "$tags" }, |
{ $sortByCount: "$tags" } |
], |
"categorizedByPrice": [ |
// Filter out documents without a price e.g., _id: 7 |
{ $match: { price: { $exists: 1 } } }, |
{ |
$bucket: { |
groupBy: "$price", |
boundaries: [ 0, 150, 200, 300, 400 ], |
default: "Other", |
output: { |
"count": { $sum: 1 }, |
"titles": { $push: "$title" } |
} |
} |
} |
], |
"categorizedByYears[Auto]": [ |
{ |
$bucketAuto: { |
groupBy: "$year", |
buckets: 4 |
} |
} |
] |
} |
} |
]] |
2db.artwork.aggregate[ [ |
{ |
$facet: { |
"categorizedByTags": [ |
{ $unwind: "$tags" }, |
{ $sortByCount: "$tags" } |
], |
"categorizedByPrice": [ |
// Filter out documents without a price e.g., _id: 7 |
{ $match: { price: { $exists: 1 } } }, |
{ |
$bucket: { |
groupBy: "$price", |
boundaries: [ 0, 150, 200, 300, 400 ], |
default: "Other", |
output: { |
"count": { $sum: 1 }, |
"titles": { $push: "$title" } |
} |
} |
} |
], |
"categorizedByYears[Auto]": [ |
{ |
$bucketAuto: { |
groupBy: "$year", |
buckets: 4 |
} |
} |
] |
} |
} |
]] |
3
Mỗi đường dẫn phụ trong $facet
được thông qua cùng một tập hợp các tài liệu đầu vào. Các đường dẫn phụ này hoàn toàn độc lập với nhau và đầu ra mảng tài liệu được lưu trữ trong các trường riêng biệt trong tài liệu đầu ra. Đầu ra của một đường dẫn phụ không thể được sử dụng làm đầu vào cho một đường dẫn phụ khác trong cùng một giai đoạn $facet
. Nếu cần thêm các tập hợp, hãy thêm các giai đoạn bổ sung sau $facet
và chỉ định tên trường,
db.artwork.aggregate[ [ |
{ |
$facet: { |
"categorizedByTags": [ |
{ $unwind: "$tags" }, |
{ $sortByCount: "$tags" } |
], |
"categorizedByPrice": [ |
// Filter out documents without a price e.g., _id: 7 |
{ $match: { price: { $exists: 1 } } }, |
{ |
$bucket: { |
groupBy: "$price", |
boundaries: [ 0, 150, 200, 300, 400 ], |
default: "Other", |
output: { |
"count": { $sum: 1 }, |
"titles": { $push: "$title" } |
} |
} |
} |
], |
"categorizedByYears[Auto]": [ |
{ |
$bucketAuto: { |
groupBy: "$year", |
buckets: 4 |
} |
} |
] |
} |
} |
]] |
7, của đầu ra đường dẫn phụ mong muốn.
Giai đoạn $facet
và các pipelines phụ của nó, không thể sử dụng các chỉ mục, ngay cả khi các ống con của nó sử dụng
db.artwork.aggregate[ [ |
{ |
$facet: { |
"categorizedByTags": [ |
{ $unwind: "$tags" }, |
{ $sortByCount: "$tags" } |
], |
"categorizedByPrice": [ |
// Filter out documents without a price e.g., _id: 7 |
{ $match: { price: { $exists: 1 } } }, |
{ |
$bucket: { |
groupBy: "$price", |
boundaries: [ 0, 150, 200, 300, 400 ], |
default: "Other", |
output: { |
"count": { $sum: 1 }, |
"titles": { $push: "$title" } |
} |
} |
} |
], |
"categorizedByYears[Auto]": [ |
{ |
$bucketAuto: { |
groupBy: "$year", |
buckets: 4 |
} |
} |
] |
} |
} |
]] |
9 hoặc nếu $facet
là giai đoạn đầu tiên trong đường ống. Giai đoạn $facet
sẽ luôn thực hiện { |
"categorizedByYears[Auto]" : [ |
// First bucket includes the document without a year, e.g., _id: 4 |
{ "_id" : { "min" : null, "max" : 1902 }, "count" : 2 }, |
{ "_id" : { "min" : 1902, "max" : 1918 }, "count" : 2 }, |
{ "_id" : { "min" : 1918, "max" : 1926 }, "count" : 2 }, |
{ "_id" : { "min" : 1926, "max" : 1931 }, "count" : 2 } |
], |
"categorizedByPrice" : [ |
{ |
"_id" : 0, |
"count" : 2, |
"titles" : [ |
"Dancer", |
"Blue Flower" |
] |
}, |
{ |
"_id" : 150, |
"count" : 2, |
"titles" : [ |
"The Pillars of Society", |
"The Great Wave off Kanagawa" |
] |
}, |
{ |
"_id" : 200, |
"count" : 1, |
"titles" : [ |
"Melancholy III" |
] |
}, |
{ |
"_id" : 300, |
"count" : 1, |
"titles" : [ |
"Composition VII" |
] |
}, |
{ |
// Includes document price outside of bucket boundaries, e.g., _id: 5 |
"_id" : "Other", |
"count" : 1, |
"titles" : [ |
"The Persistence of Memory" |
] |
} |
], |
"categorizedByTags" : [ |
{ "_id" : "painting", "count" : 6 }, |
{ "_id" : "oil", "count" : 4 }, |
{ "_id" : "Expressionism", "count" : 3 }, |
{ "_id" : "Surrealism", "count" : 2 }, |
{ "_id" : "abstract", "count" : 2 }, |
{ "_id" : "woodblock", "count" : 1 }, |
{ "_id" : "woodcut", "count" : 1 }, |
{ "_id" : "ukiyo-e", "count" : 1 }, |
{ "_id" : "satire", "count" : 1 }, |
{ "_id" : "caricature", "count" : 1 } |
] |
} |
2 trong quá trình thực hiện.Hãy xem xét một cửa hàng trực tuyến có hàng tồn kho được lưu trữ trong bộ sưu tập
{ |
"categorizedByYears[Auto]" : [ |
// First bucket includes the document without a year, e.g., _id: 4 |
{ "_id" : { "min" : null, "max" : 1902 }, "count" : 2 }, |
{ "_id" : { "min" : 1902, "max" : 1918 }, "count" : 2 }, |
{ "_id" : { "min" : 1918, "max" : 1926 }, "count" : 2 }, |
{ "_id" : { "min" : 1926, "max" : 1931 }, "count" : 2 } |
], |
"categorizedByPrice" : [ |
{ |
"_id" : 0, |
"count" : 2, |
"titles" : [ |
"Dancer", |
"Blue Flower" |
] |
}, |
{ |
"_id" : 150, |
"count" : 2, |
"titles" : [ |
"The Pillars of Society", |
"The Great Wave off Kanagawa" |
] |
}, |
{ |
"_id" : 200, |
"count" : 1, |
"titles" : [ |
"Melancholy III" |
] |
}, |
{ |
"_id" : 300, |
"count" : 1, |
"titles" : [ |
"Composition VII" |
] |
}, |
{ |
// Includes document price outside of bucket boundaries, e.g., _id: 5 |
"_id" : "Other", |
"count" : 1, |
"titles" : [ |
"The Persistence of Memory" |
] |
} |
], |
"categorizedByTags" : [ |
{ "_id" : "painting", "count" : 6 }, |
{ "_id" : "oil", "count" : 4 }, |
{ "_id" : "Expressionism", "count" : 3 }, |
{ "_id" : "Surrealism", "count" : 2 }, |
{ "_id" : "abstract", "count" : 2 }, |
{ "_id" : "woodblock", "count" : 1 }, |
{ "_id" : "woodcut", "count" : 1 }, |
{ "_id" : "ukiyo-e", "count" : 1 }, |
{ "_id" : "satire", "count" : 1 }, |
{ "_id" : "caricature", "count" : 1 } |
] |
} |
3 sau:{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
Hoạt động sau đây sử dụng các tính năng khía cạnh của MongoDB để cung cấp cho khách hàng hàng tồn kho của cửa hàng được phân loại theo nhiều chiều như thẻ, giá và năm được tạo. Giai đoạn $facet
này có ba hình con con sử dụng
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
5, { "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
3 hoặc { "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
4 để thực hiện tập hợp đa diện này. Các tài liệu đầu vào từ { |
"categorizedByYears[Auto]" : [ |
// First bucket includes the document without a year, e.g., _id: 4 |
{ "_id" : { "min" : null, "max" : 1902 }, "count" : 2 }, |
{ "_id" : { "min" : 1902, "max" : 1918 }, "count" : 2 }, |
{ "_id" : { "min" : 1918, "max" : 1926 }, "count" : 2 }, |
{ "_id" : { "min" : 1926, "max" : 1931 }, "count" : 2 } |
], |
"categorizedByPrice" : [ |
{ |
"_id" : 0, |
"count" : 2, |
"titles" : [ |
"Dancer", |
"Blue Flower" |
] |
}, |
{ |
"_id" : 150, |
"count" : 2, |
"titles" : [ |
"The Pillars of Society", |
"The Great Wave off Kanagawa" |
] |
}, |
{ |
"_id" : 200, |
"count" : 1, |
"titles" : [ |
"Melancholy III" |
] |
}, |
{ |
"_id" : 300, |
"count" : 1, |
"titles" : [ |
"Composition VII" |
] |
}, |
{ |
// Includes document price outside of bucket boundaries, e.g., _id: 5 |
"_id" : "Other", |
"count" : 1, |
"titles" : [ |
"The Persistence of Memory" |
] |
} |
], |
"categorizedByTags" : [ |
{ "_id" : "painting", "count" : 6 }, |
{ "_id" : "oil", "count" : 4 }, |
{ "_id" : "Expressionism", "count" : 3 }, |
{ "_id" : "Surrealism", "count" : 2 }, |
{ "_id" : "abstract", "count" : 2 }, |
{ "_id" : "woodblock", "count" : 1 }, |
{ "_id" : "woodcut", "count" : 1 }, |
{ "_id" : "ukiyo-e", "count" : 1 }, |
{ "_id" : "satire", "count" : 1 }, |
{ "_id" : "caricature", "count" : 1 } |
] |
} |
3 chỉ được tìm nạp từ cơ sở dữ liệu một lần, khi bắt đầu hoạt động:$facet
stage has three sub-pipelines that use
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
5, { "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
3, or { "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, |
"price" : NumberDecimal["199.99"], |
"tags" : [ "painting", "satire", "Expressionism", "caricature" ] } |
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, |
"price" : NumberDecimal["280.00"], |
"tags" : [ "woodcut", "Expressionism" ] } |
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, |
"price" : NumberDecimal["76.04"], |
"tags" : [ "oil", "Surrealism", "painting" ] } |
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", |
"price" : NumberDecimal["167.30"], |
"tags" : [ "woodblock", "ukiyo-e" ] } |
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, |
"price" : NumberDecimal["483.00"], |
"tags" : [ "Surrealism", "painting", "oil" ] } |
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, |
"price" : NumberDecimal["385.00"], |
"tags" : [ "oil", "painting", "abstract" ] } |
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, |
"tags" : [ "Expressionism", "painting", "oil" ] } |
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, |
"price" : NumberDecimal["118.42"], |
"tags" : [ "abstract", "painting" ] } |
4 to perform this multi-faceted aggregation.
The input documents from { |
"categorizedByYears[Auto]" : [ |
// First bucket includes the document without a year, e.g., _id: 4 |
{ "_id" : { "min" : null, "max" : 1902 }, "count" : 2 }, |
{ "_id" : { "min" : 1902, "max" : 1918 }, "count" : 2 }, |
{ "_id" : { "min" : 1918, "max" : 1926 }, "count" : 2 }, |
{ "_id" : { "min" : 1926, "max" : 1931 }, "count" : 2 } |
], |
"categorizedByPrice" : [ |
{ |
"_id" : 0, |
"count" : 2, |
"titles" : [ |
"Dancer", |
"Blue Flower" |
] |
}, |
{ |
"_id" : 150, |
"count" : 2, |
"titles" : [ |
"The Pillars of Society", |
"The Great Wave off Kanagawa" |
] |
}, |
{ |
"_id" : 200, |
"count" : 1, |
"titles" : [ |
"Melancholy III" |
] |
}, |
{ |
"_id" : 300, |
"count" : 1, |
"titles" : [ |
"Composition VII" |
] |
}, |
{ |
// Includes document price outside of bucket boundaries, e.g., _id: 5 |
"_id" : "Other", |
"count" : 1, |
"titles" : [ |
"The Persistence of Memory" |
] |
} |
], |
"categorizedByTags" : [ |
{ "_id" : "painting", "count" : 6 }, |
{ "_id" : "oil", "count" : 4 }, |
{ "_id" : "Expressionism", "count" : 3 }, |
{ "_id" : "Surrealism", "count" : 2 }, |
{ "_id" : "abstract", "count" : 2 }, |
{ "_id" : "woodblock", "count" : 1 }, |
{ "_id" : "woodcut", "count" : 1 }, |
{ "_id" : "ukiyo-e", "count" : 1 }, |
{ "_id" : "satire", "count" : 1 }, |
{ "_id" : "caricature", "count" : 1 } |
] |
} |
3 are fetched from the database only once, at the beginning of the operation:db.artwork.aggregate[ [ |
{ |
$facet: { |
"categorizedByTags": [ |
{ $unwind: "$tags" }, |
{ $sortByCount: "$tags" } |
], |
"categorizedByPrice": [ |
// Filter out documents without a price e.g., _id: 7 |
{ $match: { price: { $exists: 1 } } }, |
{ |
$bucket: { |
groupBy: "$price", |
boundaries: [ 0, 150, 200, 300, 400 ], |
default: "Other", |
output: { |
"count": { $sum: 1 }, |
"titles": { $push: "$title" } |
} |
} |
} |
], |
"categorizedByYears[Auto]": [ |
{ |
$bucketAuto: { |
groupBy: "$year", |
buckets: 4 |
} |
} |
] |
} |
} |
]] |
Hoạt động trả về tài liệu sau:
{ |
"categorizedByYears[Auto]" : [ |
// First bucket includes the document without a year, e.g., _id: 4 |
{ "_id" : { "min" : null, "max" : 1902 }, "count" : 2 }, |
{ "_id" : { "min" : 1902, "max" : 1918 }, "count" : 2 }, |
{ "_id" : { "min" : 1918, "max" : 1926 }, "count" : 2 }, |
{ "_id" : { "min" : 1926, "max" : 1931 }, "count" : 2 } |
], |
"categorizedByPrice" : [ |
{ |
"_id" : 0, |
"count" : 2, |
"titles" : [ |
"Dancer", |
"Blue Flower" |
] |
}, |
{ |
"_id" : 150, |
"count" : 2, |
"titles" : [ |
"The Pillars of Society", |
"The Great Wave off Kanagawa" |
] |
}, |
{ |
"_id" : 200, |
"count" : 1, |
"titles" : [ |
"Melancholy III" |
] |
}, |
{ |
"_id" : 300, |
"count" : 1, |
"titles" : [ |
"Composition VII" |
] |
}, |
{ |
// Includes document price outside of bucket boundaries, e.g., _id: 5 |
"_id" : "Other", |
"count" : 1, |
"titles" : [ |
"The Persistence of Memory" |
] |
} |
], |
"categorizedByTags" : [ |
{ "_id" : "painting", "count" : 6 }, |
{ "_id" : "oil", "count" : 4 }, |
{ "_id" : "Expressionism", "count" : 3 }, |
{ "_id" : "Surrealism", "count" : 2 }, |
{ "_id" : "abstract", "count" : 2 }, |
{ "_id" : "woodblock", "count" : 1 }, |
{ "_id" : "woodcut", "count" : 1 }, |
{ "_id" : "ukiyo-e", "count" : 1 }, |
{ "_id" : "satire", "count" : 1 }, |
{ "_id" : "caricature", "count" : 1 } |
] |
} |