• 使用Mongo官方驱动操作Mongo数据库


    首先到 https://github.com/mongodb/mongo-csharp-driver/downloads 下载Mongo官方驱动

    下载完成后引用到项目中

        public  class ConnHelp
        {
            public static string ConnectionString = "mongodb://127.0.0.1:27017/qiao";
            public static MongoDatabase GetDatabaseFromUrl(MongoUrl url)
            {
                var client = new MongoClient(url);
                var server = client.GetServer();
                return server.GetDatabase(url.DatabaseName);
            }
            public static MongoDatabase GetDatabaseFromConnectionString(string connectionstring)
            {
                return GetDatabaseFromUrl(new MongoUrl(connectionstring));
            }
            public static MongoCollection<T> GetCollection<T>(string collectionName)
            {
                return GetDatabaseFromConnectionString(ConnectionString).GetCollection<T>(collectionName);
            }
        }
    using MongoDB.Bson;
    using MongoDB.Driver;
    using MongoDB.Driver.Builders;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MognoLearn
    {
        class Program
        {
            static void Main(string[] args)
            {
                MongoCollection<aa> tt = ConnHelp.GetCollection<aa>("aa");
    
                //List<aa> = tt.FindAll().ToList();
                var query = Query.And(Query<aa>.EQ(x => x.name,"张三"));
                //{ "name" : "张三" }
                List<aa> ll = tt.Find(query).ToList();//根据 query 的条件返回数据
                List<aa> list = tt.FindAll().ToList();//查询 全部的数据
                List<string> str = new List<string>();
                str.Add("张三");
                str.Add("李四");
                query = Query.And(
                        Query<aa>.In(x=>x.name,str)
                        );
                //{ "name" : { "$in" : ["张三", "李四"] } }
                list = tt.Find(query).ToList();
                
    
    
                //对数据库中的内嵌文档books进行聚合
                //如果需要过滤掉一些数据在这个地方写过滤条件
                //var match = new BsonDocument 
                //{ 
                //    { 
                //        "$match",
                //        query.ToBsonDocument() 
                //    } 
                //};
    
                var unwind = new BsonDocument 
                {
                    {
                        "$unwind",
                        "$books"
                    }
                };
                //unwind 后面也可以写一些过滤条件,用来过滤掉一些拆分后可能不需要的数据
                var group = new BsonDocument
                {
                   { 
                        "$group",                        
                        new BsonDocument
                        {
                             {
                                 "_id", new BsonDocument 
                                 { 
                                     { "books","$books.name" },
                                     { "money","$money" }
                                 }
                             },
                             {
                                 "Count", new BsonDocument
                                 {
                                    { "$sum" , 1}
                                 }
                             }
                        }
                    }
                };
                AggregateResult use = tt.Aggregate(unwind, group);
                var sttr = use.Response.ToList()[0].Value;
                //{[{ "_id" : { "books" : "生物" }, "Count" : 1 }, { "_id" : { "books" : "地理" }, "Count" : 1 }, { "_id" : { "books" : "化学" }, "Count" : 4 },
             { "_id" : { "books" : "英语" }, "Count" : 2 }, { "_id" : { "books" : "语文" }, "Count" : 7 }, { "_id" : { "books" : "数学" }, "Count" : 2 }, { "_id" : { "books" : "mongo权威指南" }, "Count" : 1 }]}
    Console.ReadLine(); } } }
    namespace MognoLearn
    {
        public class aa
        {
            //[BsonId]
            [BsonRepresentation(BsonType.ObjectId)]
            public string id { get; set; }
            public string name { get; set; }
            public List<book> books { get; set; }
            //[BsonDateTimeOptions(Kind = DateTimeKind.Local)] 
            //public DateTime AddTime { get; set; }
        }
    
        public class book
        {
            public string name { get; set; }
            public double money { get; set; }
            public int page { get; set; }
        }
    }
  • 相关阅读:
    input设置只能输入数字,附上手机号码验证
    vue组件传值的几种方式
    微信小程序之子组件的使用
    vue ckeditor5 高度的设置
    vue v-if与v-show的深入思考
    vue与react之间的区别
    vue打印电子面单(后台传来的html的解决方案)
    Asp.NET 与 WebApi 共享Session
    Python大数据:jieba 中文分词,词频统计
    Python大数据:信用卡逾期分析
  • 原文地址:https://www.cnblogs.com/ansheng/p/5481690.html
Copyright © 2020-2023  润新知