C# / .Net Core 访问MongoDb库
话不多说直接上代码
连接字符串:
{
"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"));读取配置文件
C# / .Net Core 访问MongoDb库的更多相关文章
- net Core 通过 Ef Core 访问、管理Mysql
net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...
- 使用Spring访问Mongodb的方法大全——Spring Data MongoDB查询指南
1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...
- 使用Entity Framework Core访问数据库(DB2篇)
前言 上一篇讲了一些EF Core访问Oracle的坑.(感兴趣请移步:使用Entity Framework Core访问数据库(Oracle篇)) 这篇主要讲一下关于EF Core访问DB2的一揽子 ...
- 使用Entity Framework Core访问数据库(Oracle篇)
前言 哇..看看时间 真的很久很久没写博客了 将近一年了. 最近一直在忙各种家中事务和公司的新框架 终于抽出时间来更新一波了. 本篇主要讲一下关于Entity Framework Core访问ora ...
- 使用spring boot访问mongodb数据库
一. spring boot中传参的方法 1.自动化配置 spring Boot 对于开发人员最大的好处在于可以对 Spring 应用进行自动配置.Spring Boot 会根据应用中声明的第三方依赖 ...
- 使用Spring访问Mongodb的方法大全——Spring Data MongoDB
1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...
- ImageSharp .NET Core跨平台图形处理库
ImageSharp 是支持.NET Core跨平台图形处理库,ImageSharp是ImageProcessor 的.NET Core跨平台实现. ImageSharp 支持如下操作: 调整大小,裁 ...
- Web---JSTL(Java标准标签库)-Core核心标签库、I18N国际化、函数库
前面为JSTL中的常用EL函数,后面的为具体演示实例! JSTL简介: JSTL(Java Standard Tag Library) –Java标准标签库. SUN公司制定的一套标准标签库的规范. ...
- VisualSVN Server安装后,TortoiseSVN远程无法访问版本库。
修正!重演了一遍,发现总结有误,重新整理下.首先访问版本库的路径不清楚的话可以在VisualSVN Server的版本库上右键“Copy URL to Clipboard”.访问版本库失败的几种情况: ...
随机推荐
- Redhat下 Apache, php, mysql的默认安装路径
apache: 如果采用RPM包安装,安装路径应在 /etc/httpd目录下 apache配置文件:/etc/httpd/conf/httpd.conf Apache模块路径:/usr/sbin/a ...
- HTML5视频直播及H5直播扫盲
章来源:http://geek.csdn.net/news/detail/95188 分享内容简介: 目前视频直播,尤其是移动端的视频直播已经火到不行了,基本上各大互联网公司都有了自己的直播产品,所以 ...
- java代码求奖金。要求从键盘输入利润
总结:看似文字描述很多, package com.ai; import java.util.Scanner; import com.b.Scaner; //v企业发放的奖金根据利润提成.利润(I)低于 ...
- crush class实验
标签(空格分隔): ceph,ceph实验,crushmap luminous版本的ceph新增了一个功能crush class,这个功能又可以称为磁盘智能分组.因为这个功能就是根据磁盘类型自动的进行 ...
- 关于 NULL的坑
有如下的表: select * from testtable where name in ('name'): 结果是第一条: select * from testtable where name n ...
- Deep Learning 学习笔记(5):Regularization 规则化
过拟合(overfitting): 实际操作过程中,无论是线性回归还是逻辑回归,其假设函数h(x)都是人为设定的(尽管可以通过实验选择最优). 这样子就可能出线“欠拟合”或者“过拟合”现象. 所谓过拟 ...
- python--open用法
open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 如:' ...
- Android使用简单的Service
首先要自定义一个Service,设定它在后台要干什么. public class MyService extends Service { @Nullable @Override public IBin ...
- 2017年排名前15的数据科学python库
2017年排名前15的数据科学python库 2017-05-22 Python程序员 Python程序员 Python程序员 微信号 pythonbuluo 功能介绍 最专业的Python社区,有每 ...
- ubuntu网速慢解决方法
ubuntu网速慢解决方法 (2011-04-02 09:58:21) 本人在Window7下装ubuntu10.10双系统,在window7下速度挺快的,到了ubuntu速度就慢了很 ...