话不多说直接上代码

连接字符串:

{
"AppSettings": {
"mongodb": "mongodb://用户名:密码@IP地址:端口号"
}
}

主体代码:

 using ABCDEFG.Config;
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(ABCDEFG.GetAppSetting("mongodb"));
} public MongoContext(string connectionName)
{
Client = new MongoClient(MengTConfig.GetAppSetting(connectionName));
} 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
{
public static void Add<T>(this IMongoCollection<T> collenction, T Model) where T : IMongoModel
=> collenction.InsertOne(Model); 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);
}
}
}

IMongoModel:

 using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Text; namespace Mongodb
{
public partial class IMongoModel
{
/// <summary>
/// 基础ID
/// </summary>
public ObjectId _id { get; set; }
}
}

值得注意的是:需引用MongoDB.BSon与MongoDB.Driver

VS2017若在引用包后,还是无法找到命名空间,重启VS即可。

ABCDEFG.GetAppSetting("mongodb"));读取配置文件

最新文章

  1. 理解OVER子句
  2. iOS常用设计模式和机制之Block简单使用
  3. WindowXP与WIN7环境安装、破解、配置AppScan8.0
  4. spark sql cache
  5. (转)JS浮动窗口(随浏览器滚动而滚动)
  6. 545B. Equidistant String
  7. TCP 滑动窗口和 拥塞窗口
  8. 那些经常被遗忘的 Java 面试题
  9. Automotive Security的一些资料和心得(4):Automotive Safeguards
  10. 大暴力——[HAOI]2012音量调节
  11. IHttpModule与IHttpHandler的区别整理
  12. 将bat文件或exe程序注册成windows服务
  13. 170113、CentOs6.4中安装和配置vsftp简明教程
  14. SpringtMVC运行流程:@RequestMapping 方法中的 Map、HttpServletRequest等参数信息是如何封装和传递的(源码理解)
  15. 雷林鹏分享:jQuery EasyUI 数据网格 - 创建复杂工具栏
  16. NOIP2012疫情控制(二分答案+树上贪心)
  17. JSTL-简介
  18. win server 2008 R2 安装IIS
  19. OpenGL chapter5 基础纹理
  20. Android之系统架构

热门文章

  1. chrome浏览器,调试详解,调试js、调试php、调试ajax
  2. python开发函数进阶:内置函数
  3. 操盘策略:KDJ三线合一 必定孕育大牛股
  4. 何时会发生db file sequential read等待事件?
  5. MySQL 性能测试经验
  6. oracle插入
  7. C Primer Plus学习笔记(十一)- 存储类别、链接和内存管理
  8. CAD库中统计PBN运行航路条数和总距离
  9. XML注释与Description标签及Java:注解(Annotation)的关系
  10. Bokeh绘图