Xử lý nhiều trong một giai đoạn duy nhất trên cùng một bộ tài liệu đầu vào. Mỗi đường ống phụ có trường riêng trong tài liệu đầu ra nơi kết quả của nó được lưu trữ dưới dạng một mảng tài liệu
Giai đoạn $facet
cho phép bạn tạo các tổng hợp nhiều mặt đặc trưng cho dữ liệu trên nhiều thứ nguyên hoặc khía cạnh, trong một giai đoạn tổng hợp duy nhất. Tập hợp nhiều mặt cung cấp nhiều bộ lọc và phân loại để hướng dẫn duyệt và phân tích dữ liệu. Các nhà bán lẻ thường sử dụng faceting để thu hẹp kết quả tìm kiếm bằng cách tạo các bộ lọc về giá sản phẩm, nhà sản xuất, kích thước, v.v.
Chứng từ đầu vào chỉ được chuyển đến giai đoạn $facet
một lần. $facet
cho phép tổng hợp nhiều loại trên cùng một bộ tài liệu đầu vào mà không cần truy xuất tài liệu đầu vào nhiều lần
Sân khấu có dạng như sau
sao chép
{ $facet: { : [ , , ... ], : [ , , ... ], ... } }
Chỉ định tên trường đầu ra cho từng đường ống được chỉ định
Cư xử
Các giai đoạn tổng hợp liên quan đến khía cạnh phân loại và nhóm 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" ] }0 của quy trình phụ khác nhau để thực hiện tổng hợp nhiều khía cạnh
Other cũng có thể được sử dụng với $facet
với các ngoại lệ sau
Mỗi quy trình phụ trong $facet
được chuyển cùng một bộ tài liệu đầu vào chính xác. Các quy trình phụ này hoàn toàn độc lập với nhau và đầu ra mảng tài liệu của mỗi quy trình được lưu trữ trong các trường riêng biệt trong tài liệu đầu ra. Không thể sử dụng đầu ra của một quy trình phụ làm đầu vào cho một quy trình phụ khác trong cùng một giai đoạn $facet
. Nếu cần tổng hợp thêm, 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 } } ] } } ]]5, của đầu ra quy trình phụ mong muốn
Sử dụng chỉ mục
Giai đoạn $facet
và các quy trình phụ của nó không thể sử dụng các chỉ mục, ngay cả khi các quy trình phụ của nó sử dụng hoặc nếu $facet
là giai đoạn đầu tiên trong quy trình. Giai đoạn $facet
sẽ luôn thực hiện mộ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 } ] }0 trong khi thực hiện
Thí dụ
Hãy xem xét một cửa hàng trực tuyến có hàng trong 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 } ] }1 sau đây
sao chép
{ "_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" ] }
Thao tác sau đây sử dụng các tính năng tạo khía cạnh của MongoDB để cung cấp cho khách hàng khoảng không quảng cáo của cửa hàng được phân loại theo nhiều thứ nguyên, chẳng hạn như thẻ, giá và năm tạo. Giai đoạn này có ba quy trình phụ sử dụng , hoặc để thực hiện tổng hợp nhiều mặt 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 } ] }1 chỉ được lấy từ cơ sở dữ liệu một lần, khi bắt đầu hoạt động
sao chép
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
sao ché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 } ] }
← $currentOp [tập hợp] $geoNear [tập hợp] →
© MongoDB, Inc 2008-nay. MongoDB, Mongo và logo chiếc lá là các nhãn hiệu đã đăng ký của MongoDB, Inc