使用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; }
    }
}

 

posted @ 2016-05-11 14:08  乔安生  阅读(429)  评论(0编辑  收藏  举报