Net Core 学习入门(六)----------配置mongoDB

1,在官网处下载最新安装包,安装到windows。

2,使用vs新建一个项目,并使用nugut导入mongoDb的开发包。

3,配置类

     配置是按照官网的教程配的,操作类是网上其他大神的,

using CoreMvc.Common;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace Mongodb
{
    public class MongoContext
    {
        public MongoContext()
        {
            Client = new MongoClient("mongodb://localhost:27017");
        }

        public MongoContext(string connectionName)
        {
            Client = new MongoClient("mongodb://localhost:27017,localhost:27018,localhost:27019");
        }

        private MongoClient Client { get; set; }

        private IMongoDatabase DataBase { get => Client.GetDatabase("MengTLog"); }

        public IMongoCollection<T> DbSet<T>() where T : IMongoModel => DataBase.GetCollection<T>("MengTLog.Logger");

    }

    public static class MongoExtend
    {
        /// <summary>
        /// 添加一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collenction"></param>
        /// <param name="Model"></param>
        public static void Add<T>(this IMongoCollection<T> collenction, T Model) where T : IMongoModel
                  => collenction.InsertOne(Model);

        /// <summary>
        /// 添加一对
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collenction"></param>
        /// <param name="Model"></param>
        public static void AddList<T>(this IMongoCollection<T> collenction, List<T> Model) where T : IMongoModel
                 => collenction.InsertMany(Model);

        /// <summary>
        /// 查找第一个
        /// </summary>
        public static T FirstOrDefault<T>(this IMongoCollection<T> collenction, Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            return collenction.Find(expression).FirstOrDefault();
        }

        /// <summary>
        /// 查找符合数据列表
        /// </summary>
        public static List<T> FindToList<T>(this IMongoCollection<T> collenction, Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            return collenction.Find(expression).ToList();
        }

        /// <summary>
        /// 删除全部匹配数据
        /// </summary>
        public static void Delete<T>(this IMongoCollection<T> collenction, Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            collenction.DeleteManyAsync(expression);
        }


        /// <summary>
        /// 删除一个
        /// </summary>
        public static void DeleteOne<T>(this IMongoCollection<T> collenction, Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            collenction.DeleteOneAsync(expression);
        }
    }
}
using CoreMvc.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoreMvc.Models
{
    public class Product:IMongoModel
    {
       // public int Id { set; get; }

        public string Name { set; get; }
    }
}
using CoreMvc.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace CoreMvc.Models
{
    public class Product:IMongoModel
    {
       // public int Id { set; get; }


        public string Name { set; get; }
    }
}

4,方法调用

   public IActionResult About()
        {
            MongoContext context = new MongoContext();
            //插入
            var p = context.DbSet<Product>();
            p.Add(new Product { Name = "2324232" });
            //查询
            var str = p.FirstOrDefault(c => c.Name != "").Name;

            ViewData["Message"] = str;

            return View();
        }


猜你喜欢

转载自blog.csdn.net/weixin_41609327/article/details/80878062