Using Facets in the Aggregation Framework C# with

2019-06-12 07:54发布

问题:

How can I represent this pipeline in C# ?

-----Id
-----Name
-----ProductAttributes  (object array)
     |
     -----ProductAttributeType  
     -----ProductAttributeValues  (string array)

My Collection:

{ "_id" : ObjectId("5b41a5e225cd892c14628b78"), "Name" : "Esmalte Super Pérola Fashion Glamour", "ProductAttributes" : [ { "ProductAttributeType" : "Coleção", "Values" : [ "Fashion" ] }, { "ProductAttributeType" : "Tamanho", "Values" : [ "8 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Vermelho", "Cremoso" ] } ] }
{ "_id" : ObjectId("5b41a5e225cd892c14628b79"), "Name" : "Esmalte Fina Flor Azul 97", "ProductAttributes" : [ { "ProductAttributeType" : "Tamanho", "Values" : [ "8 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Azul", "Cremoso" ] } ] }
{ "_id" : ObjectId("5b41a5e225cd892c14628b7a"), "Name" : "Esmalte Fina Flor Matte Cashmere", "ProductAttributes" : [ { "ProductAttributeType" : "Coleção", "Values" : [ "Matte" ] }, { "ProductAttributeType" : "Tamanho", "Values" : [ "8 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Verde", "Matte", "Fosco" ] } ] }
{ "_id" : ObjectId("5b41a5e325cd892c14628b7b"), "Name" : "Esmalte Fina Flor Dany", "ProductAttributes" : [ { "ProductAttributeType" : "Tamanho", "Values" : [ "8 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Azul" ] } ] }
{ "_id" : ObjectId("5b41a5e325cd892c14628b7c"), "Name" : "Esmalte Fina Flor Londres", "ProductAttributes" : [ { "ProductAttributeType" : "Tamanho", "Values" : [ "8 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Chumbo", "Perolado", "Cintilante" ] } ] }
{ "_id" : ObjectId("5b41a5e325cd892c14628b7d"), "Name" : "Esmalte Fina Flor Paris", "ProductAttributes" : [ { "ProductAttributeType" : "Tamanho", "Values" : [ "8 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Roxo", "Perolado" ] } ] }
{ "_id" : ObjectId("5b41a5e425cd892c14628b7e"), "Name" : "Esmalte Hits 201", "ProductAttributes" : [ { "ProductAttributeType" : "Tamanho", "Values" : [ "6 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Rosa", "Cremoso" ] } ] }
{ "_id" : ObjectId("5b41a5e425cd892c14628b7f"), "Name" : "Esmalte Hits 209", "ProductAttributes" : [ { "ProductAttributeType" : "Tamanho", "Values" : [ "6 ml" ] }, { "ProductAttributeType" : "Tom", "Values" : [ "Goiaba", "Cremoso" ] } ] }

Pipeline from MongoDB Compass

[{ $unwind: { path: "$ProductAttributes"} },
 { $unwind: {  path: '$ProductAttributes.Values',} }, 
 { $facet: {        
           Tom:[            
                {$match:{"ProductAttributes.ProductAttributeType":{$eq: 'Tom'}}},                   
                {$sortByCount : '$ProductAttributes.Values'}                    
           ],
           Colecao:[
                {$match:{"ProductAttributes.ProductAttributeType":{$eq: 'Coleção'}}},
                {$sortByCount : '$ProductAttributes.Values'} 
           ]
          } 
}]

I'm trying this code without success. And I don't know how to add two facets in pipeline.

Error in line .Facet(facetPipelineTom);

Error CS0411 The type arguments for method 'IAggregateFluent.Facet(IEnumerable>, AggregateFacetOptions)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

_product is from type IMongoCollection

    var tomFilter = Builders<Product>.Filter.ElemMatch(p => p.ProductAttributes, p => p.ProductAttributeType == "Tom");
    var matchTom = PipelineStageDefinitionBuilder.Match(tomFilter);

    var colecaoFilter = Builders<Product>.Filter.ElemMatch(p => p.ProductAttributes, p => p.ProductAttributeType == "Coleção");
    var matchColecao = PipelineStageDefinitionBuilder.Match(colecaoFilter);

    var sortByCount = PipelineStageDefinitionBuilder.SortByCount<BsonDocument, BsonDocument>("$ProductAttributeType.Values");

    var pipelineTom = PipelineDefinition<Product, AggregateSortByCountResult<BsonDocument>>.Create(new IPipelineStageDefinition[] { matchTom, sortByCount });
    var pipelineColecao = PipelineDefinition<Product, AggregateSortByCountResult<BsonDocument>>.Create(new IPipelineStageDefinition[] { matchColecao, sortByCount });

    var facetPipelineTom = AggregateFacet.Create("Tom", pipelineTom);
    var facetPipelineColecao = AggregateFacet.Create("Colecao", pipelineTom);

    var pipeline = _products.Aggregate()
        .Unwind(p => p.ProductAttributes)
        .Unwind(p => p["ProductAttributes.Values"])
        .Facet(facetPipelineTom);

回答1:

One of the challenges here, I suppose, is that after the $unwind stages you immediately have to deal with BsonDocuments in your $facet stages - not Products.

Anyway, here is a working version:

var tomFilter = Builders<BsonDocument>.Filter.Eq("ProductAttributes.ProductAttributeType", "Tom");
var matchTom = PipelineStageDefinitionBuilder.Match(tomFilter);

var colecaoFilter = Builders<BsonDocument>.Filter.Eq("ProductAttributes.ProductAttributeType", "Coleção");
var matchColecao = PipelineStageDefinitionBuilder.Match(colecaoFilter);

var sortByCount = PipelineStageDefinitionBuilder.SortByCount<BsonDocument, string>("$ProductAttributes.Values");

var pipelineTom = PipelineDefinition<BsonDocument, AggregateSortByCountResult<string>>.Create(new IPipelineStageDefinition[] { matchTom, sortByCount });
var pipelineColecao = PipelineDefinition<BsonDocument, AggregateSortByCountResult<string>>.Create(new IPipelineStageDefinition[] { matchColecao, sortByCount });

var facetPipelineTom = AggregateFacet.Create("Tom", pipelineTom);
var facetPipelineColecao = AggregateFacet.Create("Colecao", pipelineColecao);

var pipeline = _products.Aggregate()
    .Unwind(p => p.ProductAttributes)
    .Unwind(p => p["ProductAttributes.Values"])
    .Facet(facetPipelineTom, facetPipelineColecao);

Console.WriteLine(pipeline.Single().Facets.ToJson());