C# MongoDB 查询,分组,聚合,排序,条件,分页
先下载个C#的驱动。MongoDB提供各种主流与非主流预言的开发驱动。
C# Driver 下载地址:这里
CSharp Driver Tutorial:这里
下载文件安装或者解压缩包
如果您是安装,请到安装位置寻找,如果是ZIP压缩包,解压缩包得到如下两个文件:
- MongoDB.Bson.dll :序列化、Json相关
- MongoDB.Driver.dll :我们的驱动
没了,只有这两个文件是我们的最爱。
有帮助点推荐
using MongoDB.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySteel.Common.Helper; using MongoDB.Driver.Builders; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson; namespace Test.MongoDB { class Program { /// <summary> /// 数据库连接 /// </summary> private const string conn = "mongodb://192.168.200.184:27017"; /// <summary> /// 指定的数据库 /// </summary> private const string dbName = "mlog"; /// <summary> /// 指定的表 /// </summary> private const string tbName = "log_table"; //创建数据连接 private static MongoServer server = MongoServer.Create(conn); //获取指定数据库 private static MongoDatabase db = server.GetDatabase(dbName); static void Main(string[] args) { //for (var index = 0; index < 2; index++) //{ // Student stu = new Student(); // stu.Name = "name" + index; // stu.Age = index; // stu.CreateDateTime = DateTime.Now.ToMillisecond(); // stu.Ques = new List<Ques>(); // stu.Ques.Add(new Ques() { Name = "qname" + index }); // Add(stu); //} Console.WriteLine("获取学生数据列表"); var list = GetStudentList(); foreach (var item in list) { Console.WriteLine(item.Name + " " + item.Age); } Console.WriteLine("获取学生姓名分组和数量"); Dictionary<string, int> group = GetStudentGroup(); foreach (var item in group) { Console.WriteLine(item.Key + " " + item.Value); } Console.Read(); } /// <summary> /// 添加 /// </summary> /// <param name="text">内容</param> /// <param name="articleId">文章ID</param> /// <param name="channelId">频道ID</param> /// <returns></returns> public static void Add(Student t) { //获取表 MongoCollection col = db.GetCollection(tbName); //插入 col.Insert(t); } public static List<Student> GetStudentList() { //创建数据连接 MongoServer server = MongoServer.Create(conn); //获取指定数据库 MongoDatabase db = server.GetDatabase(dbName); //获取表 MongoCollection<Student> col = db.GetCollection<Student>(tbName); //条件查询 var query = Query.And(Query.LTE("Age", 5)); MongoCursor<Student> mongoCursor = col.FindAs<Student>(query); //总数 Console.WriteLine(mongoCursor.Count()); mongoCursor = col.FindAs<Student>(query); //排序 mongoCursor.SetSortOrder(SortBy.Descending("Age")); //分页 mongoCursor.SetSkip(2); mongoCursor.SetLimit(2); List<Student> result = mongoCursor.ToList(); return result; } public static Dictionary<string, int> GetStudentGroup() { Dictionary<string, int> result = new Dictionary<string, int>(); result["num"] = 0; //创建数据连接 MongoServer server = MongoServer.Create(conn); //获取指定数据库 MongoDatabase db = server.GetDatabase(dbName); //获取表 MongoCollection<Student> col = db.GetCollection<Student>(tbName); GroupByBuilder groupbyBuilder = new GroupByBuilder(new string[] { "Name" }); var query = Query.And(Query.LTE("Age", 5)); var result_R = col.Group(query, groupbyBuilder, BsonDocument.Create(result), BsonJavaScript.Create("function(doc,prev){prev.num++;}"), BsonJavaScript.Create("function(doc){ doc.count=doc.num;delete doc.num; }")).ToList(); if (result_R.Count != 0) { result = new Dictionary<string, int>(); for (int i = 1; i < result_R.Count; i++) { result.Add(result_R[i]["Name"].ToString(), Convert.ToInt32(result_R[i]["count"])); } } return result; } } [BsonIgnoreExtraElements] public class Student { public string Name { get; set; } public int Age { get; set; } public List<Ques> Ques { get; set; } public long CreateDateTime { get; set; } } [BsonIgnoreExtraElements] public class Ques { public string Name { get; set; } } }
生活不易,五行缺金,求打点