我想在我的数据上创建一个汇总,以获取.Net应用程序中书籍集合的特定标签的总数。
我有以下图书课。
public class Book
{
public string Id { get; set; }
public string Name { get; set; }
[BsonDictionaryOptions(DictionaryRepresentation.Document)]
public Dictionary<string, string> Tags { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
数据保存后,将以以下格式存储在MongoDB中。
{
"_id" : ObjectId("574325a36fdc967af03766dc"),
"Name" : "My First Book",
"Tags" : {
"Edition" : "First",
"Type" : "HardBack",
"Published" : "2017",
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在MongoDB中直接使用构面,并且可以通过使用以下查询来获得所需的结果:
db.{myCollection}.aggregate(
[
{
$match: {
"Name" : "SearchValue"
}
},
{
$facet: {
"categorizedByTags" : [
{
$project :
{
Tags: { $objectToArray: "$Tags" } …Run Code Online (Sandbox Code Playgroud)