本文整理汇总了C#中MongoCollection.Aggregate方法的典型用法代码示例。如果您正苦于以下问题:C# MongoCollection.Aggregate方法的具体用法?C# MongoCollection.Aggregate怎么用?C# MongoCollection.Aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection.Aggregate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public IEnumerable<BsonDocument> Run(MongoCollection<Rental> rentals)
{
var priceRange = new BsonDocument(
"$subtract",
new BsonArray
{
"$Price",
new BsonDocument(
"$mod",
new BsonArray{"$Price",500}
)
});
var grouping =new BsonDocument(
"$group",
new BsonDocument
{
{"_id",priceRange},
{"count",new BsonDocument("$sum",1)}
});
var sort=new BsonDocument(
"$sort",
new BsonDocument("_id",1)
);
var args=new AggregateArgs
{
Pipeline=new []{grouping,sort}
};
return rentals.Aggregate(args);
}
示例2: GetStatisticItem
private StatisticItem GetStatisticItem(DateTime startStroke, DateTime endStroke, MongoCollection<Session> sessions)
{
var match = GetMatchOperator(startStroke, endStroke);
var project = GetProjectOperator(startStroke);
var project1 = GetProjectOperator1();
var group = GetGroupOperator();
var pipeline = new[] { match, project, group, project1 };
var queryResult = sessions.Aggregate(pipeline);
var items = queryResult.ResultDocuments.ToList();
var doc = queryResult.ResultDocuments.Select(BsonSerializer.Deserialize<StatisticItem>).FirstOrDefault();
var count = doc == null ? 0 : doc.Count;
var result = new StatisticItem { DateTime = startStroke, Count = count };
return result;
}
示例3: GroupByCount
/// <summary>
/// Calculate the amount of documents
/// </summary>
/// <param name="name">Field name to group by</param>
/// <returns>Document list represented by "Key" and "Value" inside aggregate result object</returns>
public AggregateResult GroupByCount(string name, MongoCollection collection, BsonDocument sort, BsonDocument limit, BsonDocument skip, BsonDocument match, bool countGroups =false)
{
// prepare pipeline operations
var group = new BsonDocument
{
{
"$group",
new BsonDocument
{
{
"_id", new BsonDocument
{
{
"Key","$" + name
}
}
},
{
"Value", new BsonDocument
{
{
"$sum", 1
}
}
}
}
}
};
var project = new BsonDocument
{
{
"$project",
new BsonDocument
{
{"_id", 0}, // remove reference to _id
{"Key","$_id.Key"}, // set reference to "_id.Name" as "UserName"
{"Value", 1}, // dont change "Count"
}
}
};
List<BsonDocument> pipeline = new List<BsonDocument>{ };
if (match != null)
pipeline.Add(match);
pipeline.Add(group);
if (countGroups)
{
var total = new BsonDocument
{
{
"$group",
new BsonDocument
{
{
"_id", 0
},
{
"Total", new BsonDocument
{
{
"$sum", 1
}
}
}
}
}
};
pipeline.Add(total);
}
else
{
pipeline.Add(project);
if (sort != null)
{
pipeline.Add(sort);
}
pipeline.Add(skip);
pipeline.Add(limit);
}
collection.EnsureIndex(IndexKeys.Ascending(name));
return collection.Aggregate(pipeline);
}