在Aggregation Framework C#中使用构面 [英] Using Facets in the Aggregation Framework C#

查看:198
本文介绍了在Aggregation Framework C#中使用构面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的数据上创建一个汇总,以获取.Net应用程序中书籍集合的特定标签的总数.

I would like to create an Aggregation on my data to get the total amount of counts for specific tags for a collection of books in my .Net application.

我有以下图书课.

public class Book
{
    public string Id { get; set; }

    public string Name { get; set; }

    [BsonDictionaryOptions(DictionaryRepresentation.Document)]
    public Dictionary<string, string> Tags { get; set; }
}

数据保存后,将以以下格式存储在MongoDB中.

And when the data is saved, it is stored in the following format in MongoDB.

{ 
    "_id" : ObjectId("574325a36fdc967af03766dc"), 
    "Name" : "My First Book", 
    "Tags" : {
        "Edition" : "First", 
        "Type" : "HardBack", 
        "Published" : "2017", 
    }
}

我一直在MongoDB中直接使用构面,并且可以通过使用以下查询来获得所需的结果:

I've been using facets directly in MongoDB and I am able to get the results that I need by using the following query:

db.{myCollection}.aggregate(
    [
        {
            $match: {
                "Name" : "SearchValue"
            }
        },
        {
            $facet: {
                 "categorizedByTags" : [ 
                     {   
                       $project :
                       { 
                         Tags: { $objectToArray: "$Tags" }
                       }
                     },
                     { $unwind : "$Tags"},
                     { $sortByCount : "$Tags"}
                  ]

            }
        },
    ]
);

但是,我无法将其转移到Mongo的.NET C#驱动程序中.如何使用.NET C#驱动程序来做到这一点?

However I am unable to transfer this over to the .NET C# Driver for Mongo. How can I do this using the .NET C# driver?

编辑-我最终将要查询有关图书其他属性的数据库,作为多面图书清单页面的一部分,例如Publisher,Author,Page count等...因此除非有更好的方法可以使用$ facet?

Edit - I will ultimately be looking to query the DB on other properties of the books as part of a faceted book listings page, such as Publisher, Author, Page count etc... hence the usage of $facet, unless there is a better way of doing this?

推荐答案

我个人不会在这里使用$facet,因为您只有一个管道,这首先会破坏$facet的目的.

I would personally not use $facet here since you've only got one pipeline which kind of defeats the purpose of $facet in the first place...

以下内容更简单且伸缩性更好($facet将创建一个可能庞大的文档).

The following is simpler and scales better ($facet will create one single potentially massive document).

db.collection.aggregate([
{
    $match: {
        "Name" : "My First Book"
    }
}, {
    $project: {
        "Tags": {
            $objectToArray: "$Tags"
        }
    }
}, {
    $unwind: "$Tags"
}, {
    $sortByCount: "$Tags"
}, {
    $group: { // not really needed unless you need to have all results in one single document
        "_id": null,
        "categorizedByTags": {
            $push: "$$ROOT"
        }
    }
}, {
    $project: { // not really needed, either: remove _id field
        "_id": 0
    }
}])

这可以使用C#驱动程序编写,如下所示:

This could be written using the C# driver as follows:

var collection = new MongoClient().GetDatabase("test").GetCollection<Book>("test");

var pipeline = collection.Aggregate()
    .Match(b => b.Name == "My First Book")
    .Project("{Tags: { $objectToArray: \"$Tags\" }}")
    .Unwind("Tags")
    .SortByCount<BsonDocument>("$Tags");

var output = pipeline.ToList().ToJson(new JsonWriterSettings {Indent = true});

Console.WriteLine(output);

以下是使用构面的版本:

Here's the version using a facet:

var collection = new MongoClient().GetDatabase("test").GetCollection<Book>("test");

var project = PipelineStageDefinitionBuilder.Project<Book, BsonDocument>("{Tags: { $objectToArray: \"$Tags\" }}");
var unwind = PipelineStageDefinitionBuilder.Unwind<BsonDocument, BsonDocument>("Tags");
var sortByCount = PipelineStageDefinitionBuilder.SortByCount<BsonDocument, BsonDocument>("$Tags");

var pipeline = PipelineDefinition<Book, AggregateSortByCountResult<BsonDocument>>.Create(new IPipelineStageDefinition[] { project, unwind, sortByCount });

// string based alternative version
//var pipeline = PipelineDefinition<Book, BsonDocument>.Create(
//    "{ $project :{ Tags: { $objectToArray: \"$Tags\" } } }",
//    "{ $unwind : \"$Tags\" }",
//    "{ $sortByCount : \"$Tags\" }");

var facetPipeline = AggregateFacet.Create("categorizedByTags", pipeline);

var aggregation = collection.Aggregate().Match(b => b.Name == "My First Book").Facet(facetPipeline);

var output = aggregation.Single().Facets.ToJson(new JsonWriterSettings { Indent = true });

Console.WriteLine(output);

这篇关于在Aggregation Framework C#中使用构面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆