问题描述
如何用C#表示此管道?
How can I represent this pipeline in C# ?
-----Id
-----Name
-----ProductAttributes (object array)
|
-----ProductAttributeType
-----ProductAttributeValues (string array)
我的收藏集:
{ "_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" ] } ] }
MongoDB Compass的管道
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.
.Facet(facetPipelineTom)行中的错误;
Error in line .Facet(facetPipelineTom);
错误CS0411无法从用法中推断出方法'IAggregateFluent.Facet(IEnumerable>,AggregateFacetOptions)'的类型参数.尝试显式指定类型参数.
Error CS0411 The type arguments for method 'IAggregateFluent.Facet(IEnumerable>, AggregateFacetOptions)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
_product来自IMongoCollection类型
_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);
推荐答案
我想这里的挑战之一是在$unwind
阶段之后,您必须立即处理$facet
阶段中的BsonDocument
-不是Product
s.
One of the challenges here, I suppose, is that after the $unwind
stages you immediately have to deal with BsonDocument
s in your $facet
stages - not Product
s.
无论如何,这是一个有效的版本:
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());
这篇关于将Aggregation Framework C#中的构面与多个构面,展开和sortByCount一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!