Net Core 學習入門(六)----------配置mongoDB
阿新 • • 發佈:2018-12-24
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(); }