MongoRepository.cs 仓储
/// <summary>
///
/// </summary>
/// <param name="filter">筛选条件
/// FilterDefinitionBuilder<BsonDocument> builderfilter = Builders<BsonDocument>.Filter;
/// List<FilterDefinition<BsonDocument>> filterlist = new List<FilterDefinition<BsonDocument>>();
/// var regexstr =request.SearchCondition.KeyWord == null ? $".*{Regex.Escape("")}.*" : $".*{Regex.Escape(request.SearchCondition.KeyWord)}.*";
/// filterlist.Add(Builders<BsonDocument>.Filter.Regex("KeyWord",new BsonRegularExpression(new Regex(regexstr, RegexOptions.IgnoreCase))));
/// filterlist.Add(Builders<BsonDocument>.Filter.Gte("SearchTime", GetUtcTime.CheckUtcTime(dts)));
/// filterlist.Add(Builders<BsonDocument>.Filter.Lte("SearchTime", GetUtcTime.CheckUtcTime(dte)));
/// </param>
/// <param name="SortField">排序字段</param>
/// <param name="SortOrder">排序方式 asc 正序 desc 倒叙</param>
/// <param name="PageIndex">当前页</param>
/// <param name="PageSize">页码容量</param>
/// <returns></returns>
public virtual async Task<IEnumerable<BsonDocument>> FindByAggregate(FilterDefinition<BsonDocument> filter,string SortField,string SortOrder,int PageIndex,int PageSize) {
return await Task.Run(() => {
//PipelineStageDefinition<BsonDocument, BsonDocument> match1 = PipelineStageDefinitionBuilder.Match<BsonDocument>(condition);
//PipelineStageDefinition<BsonDocument, BsonDocument> group = PipelineStageDefinitionBuilder.Group<BsonDocument>();
//PipelineStageDefinition<BsonDocument, BsonDocument> match2 = PipelineStageDefinitionBuilder.Match<BsonDocument>(Builders<BsonDocument>.Filter.Gte<string>("count", "1"));
//var pipelines = new List<PipelineStageDefinition<BsonDocument, BsonDocument>>() { match1, group, match2 };
//PipelineDefinition<BsonDocument, BsonDocument> pipeline1 = new PipelineStagePipelineDefinition<BsonDocument, BsonDocument>(pipelines);
BsonDocument group = new BsonDocument { { "_id", "$KeyWord" }, { "count", new BsonDocument("$sum", 1) }, { "SearchTime", new BsonDocument("$max", "$SearchTime") } };
if (!string.IsNullOrEmpty(SortField)) {
//排序生成器
SortDefinitionBuilder<BsonDocument> builderSort = Builders<BsonDocument>.Sort;
SortDefinition<BsonDocument> sort11 = builderSort.Descending(SortField);
//排序约束 Ascending 正序 Descending 倒序
if (SortOrder == "asc")
{
sort11 = builderSort.Ascending(SortField);
}
//BsonDocument sortbsd = new BsonDocument(SortField, (SortOrder == "asc" ? 1 : -1));
return _collectionDoc.Aggregate().Match(filter).Group(group).Match(Builders<BsonDocument>.Filter.Gte<int>("count", 1)).Sort(sort11).Skip((PageIndex - 1) * PageSize).Limit(PageSize).ToList();
}
return _collectionDoc.Aggregate().Match(filter).Group(group).Match(Builders<BsonDocument>.Filter.Gte<int>("count", 1)).Skip((PageIndex - 1) * PageSize).Limit(PageSize).ToList();
//var result = _collectionDoc.Aggregate().Match(filter).Group(group).Match(Builders<BsonDocument>.Filter.Gte<string>("count", "1"));
//return result.ToList<BsonDocument>();
});
}
mongo查询语句
db.HotSearchString.aggregate( [
{
$group: {
_id: "$KeyWord",
count: { $sum: 1 },
SearchTime:{$max:"$SearchTime"}
}
},
{ $match: { count: { $gte: 1 } } }
] )
网友评论