//创建约束生成器
FilterDefinitionBuilder builderFilter = Builders.Filter;
ProjectionDefinitionBuilder builderProjection = Builders.Projection;
ProjectionDefinition projection = builderProjection.Include( "HangerID" ).Include("StyleNo").Exclude( "_id" );
List result = dal.mongo.db.GetCollection("Hangers").Find( builderFilter.Empty ).Project( projection ).ToList();
Dictionary res = result.GroupBy( x => x["StyleNo"].ToString() ).Select( x => new {
StyleNo = x.Key,
Count = x.Count()
} ).OrderByDescending(x=>x.Count).Take(5).ToDictionary( x => x.StyleNo, x => x.Count );
//创建约束生成器
FilterDefinitionBuilder builderFilter = Builders.Filter;
SortDefinitionBuilder builderSort = Builders.Sort;
ProjectionDefinitionBuilder builderProjection = Builders.Projection;
ProjectionDefinition projection = builderProjection.Combine( projectionList );
SortDefinition sort = builderSort.Combine( sortList );
FilterDefinition filter = builderFilter.And( filterList );
var result = helper.collection().Find( filter ).Project( projection ).ToList();
1.BsonDocument对象
在MongoDB.Bson命名空间下存在一个BsonDocument类,它是MongoDB的文档对象,代表着MongoDB中不规则数据一条条实体模型。可以使用BsonDocument对不规则数据进行操作,这个类型继承了IEnumberable<>类,也就是说又将每一个实体模型看做一个集合,我们可以使用下标方式获取实体模型中的值
//
// 摘要:
// Gets or sets a value by position.
//
// 参数:
// index:
// The position.
//
// 返回结果:
// The value.
public override BsonValue this[int index] { get; set; }
//
// 摘要:
// Gets or sets a value by name.