sql >> Database >  >> NoSQL >> MongoDB

Facetten gebruiken in het Aggregation Framework C# met Multiple Facet, Unwind en sortByCount

Een van de uitdagingen hier, denk ik, is dat na de $unwind fasen waar je direct mee te maken krijgt BsonDocument s in je $facet stadia - niet Product v.

Hoe dan ook, hier is een werkende versie:

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());



  1. Hoe project DBref op Spring MongoDB Aggregation?

  2. Laat de nieuwe ClusterControl uw MongoDB-implementaties beveiligen

  3. Hoe implementeer ik deze mongodb-query &update-bewerking (CSharp-stuurprogramma)?

  4. Geospatiale informatie opslaan in mongoDB