C# 操作mongodb 分组
Posted shaomine
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 操作mongodb 分组相关的知识,希望对你有一定的参考价值。
c#操作mongodb的分组的简单例子:
1、首先要下载c#对应的mongodb驱动,官方下载地址:https://github.com/mongodb/mongo-csharp-driver/releases,目前版本是2.3.0
然后引用相应的命名空间,
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core;
2、实例代码:
/// <summary>
/// 数据集合,类似关系型数据库中的表
/// </summary>
static IMongoCollection<BsonDocument> _collect;
/// <summary>
/// 数据库初始化
/// </summary>
/// <param name="collectionName">数据集合名(就是数据库表名))</param>
public static void Init(string collectionName)
{
string mongoDB_Host = ConfigurationManager.AppSettings["MongoDB_Host"].ToString();
string mongoDB_DbName = ConfigurationManager.AppSettings["MongoDB_DbName"].ToString();
MongoDB.Driver.MongoClient mc = new MongoClient(mongoDB_Host);
IMongoDatabase _db = mc.GetDatabase(mongoDB_DbName);
_collect = _db.GetCollection<BsonDocument>(collectionName);
}
假设有这样一个数据集合:
{ "_id" : 1, "name" : "mike", "sex" : "男", "age" : 20 }
{ "_id" : 2, "name" : "Marry", "sex" : "女", "age" : 25 }
{ "_id" : 3, "name" : "Tom", "sex" : "女", "age" : 35 }
{ "_id" : 4, "name" : "Joe", "sex" : "女", "age" : 35 }
{ "_id" : 5, "name" : "Joe", "sex" : "女", "age" : 45 }
{ "_id" : 6, "name" : "John", "sex" : "男", "age" : 45 }
a、统计男女的数量
Sql: select $sex,count(1) from student group by $sex
BsonDocument db = new BsonDocument { { "_id", "$sex" }, { "count", new BsonDocument("$sum", 1) } };
var aggregate = _collect.Aggregate().Group(db);
List<BsonDocument> list = aggregate.ToList<BsonDocument>();
b、统计男女的总年龄
SQL: select sex,SUM(aget) totalAge from student group by sex
BsonDocument db = new BsonDocument { { "_id", "$sex" }, { "totalAge", new BsonDocument("$sum", "$age") } };
var aggregate = _collect.Aggregate().Group(db);
List<BsonDocument> list = aggregate.ToList<BsonDocument>();
c、统计男女的平均年龄,最大和最小年龄学生的信息
SQL:select sex,avg(age) avgAge from student group by sex
SQL:select sex,max(age) maxAge from student group by sex
SQL:select sex,min(age) minAge from student group by sex
//BsonDocument db = new BsonDocument { { "_id", "$sex" }, { "avgAge", new BsonDocument("$avg", "$age") } };
//BsonDocument db = new BsonDocument { { "_id", "$sex" }, { "maxAge", new BsonDocument("$max", "$age") } };
BsonDocument db = new BsonDocument { { "_id", "$sex" }, { "minAge", new BsonDocument("$min", "$age") } };
var aggregate = _collect.Aggregate().Group(db);
List<BsonDocument> list = aggregate.ToList<BsonDocument>();
d、统计年龄大于25男女的平均年龄的信息
SQL:select sex,avg(age) avgAge from student where age >25 group by sex
BsonDocument dbMatch = new BsonDocument { { "age", new BsonDocument("$gt", 25)} };
BsonDocument dbGroup = new BsonDocument { { "_id", "$sex" }, { "avgAge", new BsonDocument("$avg", "$age") } };
var aggregate = _collect.Aggregate().Match(dbMatch).Group(dbGroup);
List<BsonDocument> list = aggregate.ToList<BsonDocument>();
e、$push,$addToSet
//把男女学生的年龄放在一个集合里面,并允许年龄一样的同时存在
//BsonDocument db = new BsonDocument { { "_id", "$sex" }, { "AllAge", new BsonDocument("$push", "$age") } };
//var aggregate = _collect.Aggregate().Group(db);
//List<BsonDocument> list = aggregate.ToList<BsonDocument>();
//把男女学生的年龄放在一个集合里面,并不允许年龄一样的数据
//BsonDocument db = new BsonDocument { { "_id", "$sex" }, { "AllAge", new BsonDocument("$addToSet, "$age") } };
//var aggregate = _collect.Aggregate().Group(db);
//List<BsonDocument> list = aggregate.ToList<BsonDocument>();
以上是关于C# 操作mongodb 分组的主要内容,如果未能解决你的问题,请参考以下文章