如何用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的管道
[{ $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'}
]
}
}]
我正在尝试此代码,但未成功。而且我不知道如何在管道中添加两个方面。
.Facet(facetPipelineTom)行中的错误;
错误CS0411无法从用法中推断出方法'IAggregateFluent.Facet(IEnumerable>,AggregateFacetOptions)'的类型参数。尝试显式指定类型参数。
_product来自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);
答案 0 :(得分:1)
我认为这里的挑战之一是在$unwind
阶段之后,您必须立即处理BsonDocument
阶段中的$facet
而不是Product
无论如何,这是一个有效的版本:
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());