C# / .Net Core 访问MongoDb库
2024-08-27 06:05:37
话不多说直接上代码
连接字符串:
{
"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"));读取配置文件
最新文章
- 理解OVER子句
- iOS常用设计模式和机制之Block简单使用
- WindowXP与WIN7环境安装、破解、配置AppScan8.0
- spark sql cache
- (转)JS浮动窗口(随浏览器滚动而滚动)
- 545B. Equidistant String
- TCP 滑动窗口和 拥塞窗口
- 那些经常被遗忘的 Java 面试题
- Automotive Security的一些资料和心得(4):Automotive Safeguards
- 大暴力——[HAOI]2012音量调节
- IHttpModule与IHttpHandler的区别整理
- 将bat文件或exe程序注册成windows服务
- 170113、CentOs6.4中安装和配置vsftp简明教程
- SpringtMVC运行流程:@RequestMapping 方法中的 Map、HttpServletRequest等参数信息是如何封装和传递的(源码理解)
- 雷林鹏分享:jQuery EasyUI 数据网格 - 创建复杂工具栏
- NOIP2012疫情控制(二分答案+树上贪心)
- JSTL-简介
- win server 2008 R2 安装IIS
- OpenGL chapter5 基础纹理
- Android之系统架构