.net Framework使用之 MongoDB
新建Helper
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq; namespace WebApplication1.net
{
public class Db
{
private static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString(); private static readonly string dbName = ConfigurationManager.AppSettings["dbName"].ToString(); private static IMongoDatabase db = null; private static readonly object lockHelper = new object(); private Db() { } public static IMongoDatabase GetDb()
{
if (db == null)
{
lock (lockHelper)
{
if (db == null)
{
var client = new MongoClient(connStr);
db = client.GetDatabase(dbName);
}
}
}
return db;
}
} public class MongoDbHelper<T> where T : Users
{
private IMongoDatabase db = null; private IMongoCollection<T> collection = null; public MongoDbHelper()
{
this.db = Db.GetDb();
collection = db.GetCollection<T>(typeof(T).Name);
} public T Insert(T entity)
{
var flag = ObjectId.GenerateNewId();
entity.GetType().GetProperty("Id").SetValue(entity, flag);
entity.State = "y";
entity.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
entity.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); collection.InsertOneAsync(entity);
return entity;
} public void Modify(string id, string field, string value)
{
var filter = Builders<T>.Filter.Eq("Id", ObjectId.Parse(id));
var updated = Builders<T>.Update.Set(field, value);
UpdateResult result = collection.UpdateOneAsync(filter, updated).Result;
} public void Update(T entity)
{
var old = collection.Find(e => e.Id.Equals(entity.Id)).ToList().FirstOrDefault(); foreach (var prop in entity.GetType().GetProperties())
{
var newValue = prop.GetValue(entity);
var oldValue = old.GetType().GetProperty(prop.Name).GetValue(old);
if (newValue != null)
{
if (!newValue.ToString().Equals(oldValue.ToString()))
{
old.GetType().GetProperty(prop.Name).SetValue(old, newValue.ToString());
}
}
}
old.State = "y";
old.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var filter = Builders<T>.Filter.Eq("Id", entity.Id);
ReplaceOneResult result = collection.ReplaceOneAsync(filter, old).Result;
} public void Delete(T entity)
{
var filter = Builders<T>.Filter.Eq("Id", entity.Id);
collection.DeleteOneAsync(filter);
} public T QueryOne(string id)
{
return collection.Find(a => a.Id == ObjectId.Parse(id)).ToList().FirstOrDefault();
} public List<T> QueryAll()
{
return collection.Find(a => a.State.Equals("y")).ToList();
}
}
}
web.config加入配置

新建类
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1.net
{
public abstract class BaseEntity
{
public ObjectId Id { get; set; } public string State { get; set; } public string CreateTime { get; set; } public string UpdateTime { get; set; }
}
}
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1.net
{
public class Users:BaseEntity
{
public int UsersId { get; set; } public string Name { get; set; } }
}
使用
public ActionResult Test()
{
Users user = new Users();
user.UsersId = ;
user.Name = "张三";
//新增
var a= mg.Insert(user);
//查询
var b= mg.QueryAll();
//var c= mg.QueryOne(1.ToString());
//修改
user.Name = "张三123";
mg.Update(user); var b2 = mg.QueryAll(); //删除
mg.Delete(user); var b3 = mg.QueryAll();
return View();
}
.net Framework使用之 MongoDB的更多相关文章
- 用c#操作Mongodb(附demo)
因为需要,写了一个基于泛型的helper,这样要使用起来方便一点. 为了大家也不重复造轮子,所以发出来希望能帮到谁. 复杂的查询最好用linq,这也是mongodb官方建议的. mongodb的C#配 ...
- 关于Mongodb的全面总结
MongoDB的内部构造<MongoDB The Definitive Guide> MongoDB的官方文档基本是how to do的介绍,而关于how it worked却少之又少,本 ...
- 零售行业下MongoDB在产品目录系统、库存系统、个性推荐系统中的应用【转载】
Retail Reference Architecture Part 1: Building a Flexible, Searchable, Low-Latency Product Catalog P ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 自定义仓储之增删改查
上一篇文章(https://www.cnblogs.com/meowv/p/12913676.html)我们用Code-First的方式创建了博客所需的实体类,生成了数据库表,完成了对EF Core的 ...
- ABP vnext模块化架构的最佳实践的实现
在上一篇文章<手把手教你用Abp vnext构建API接口服务>中,我们用ABP vnext实现了WebAPI接口服务,但是并非ABP模块化架构的最佳实践.我本身也在学习ABP,我认为AB ...
- bp VNext 入门——让ABP跑起来
因好多群友@我说,ABP他们简单的了解了下,按照官方的教程一路下来跑不起来(倒在了入门的门口),才有了此文. 此文结合官方文档,一步一步带领大家让ABP跑起来(跨过门口). 建议大家一步一步实际动手操 ...
- windows类书的学习心得
原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...
- Access MongoDB Data with Entity Framework 6
This article shows how to access MongoDB data using an Entity Framework code-first approach. Entity ...
- nginx+play framework +mongoDB+redis +mysql+LBS实战总结
nginx+play framework +mongoDB+redis +mysql+LBS实战总结(一) 使用这个样的组合结构已经很久了,主要是实现web-server,不是做网站,二是纯粹的数据服 ...
随机推荐
- repeater嵌套RadioButtonList赋值
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Rep ...
- resin-pro-4.0.53报错java.lang.Error: java.lang.ClassNotFoundException: com.caucho.loader.SystemClassLoader
最初并未发现,笔者的系统环境变量JAVA_HOME变量设置错误 D:\develop\Java\x64\jdk1.8.0_144 #最初使用了阉割版的JDK 改成完整安装的JDK就可以 D:\deve ...
- Codeforces 709C 模拟
C. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 megabytes input:stand ...
- WinScp获取一个文件
CD /d C:\Program Files (x86)\WinSCPWinSCP.exe /console /command "option batch continue" &q ...
- Git 初始状操作指引
You have an empty repository To get started you will need to run these commands in your terminal. Ne ...
- mvc的表单发送ajax请求,太强大了!!!!
- css之颜色篇
app多采用浅灰#f5f5f5 白色一般用white,如果觉得白太直接了,可以加一点点灰,#fefefe, 这种情况下搭配#e4e4e4的浅灰边框最合适.
- APP自动化测试怎么测?
一般来说工具还有技术都是千篇一律 测试测到最后还是业务能力...
- 20155233 2016-2017-2 《Java程序设计》第8周学习总结
20155233 2016-2017-2 <Java程序设计>第8周学习总结 学习目标 了解NIO 会使用Channel.Buffer与NIO2 会使用日志API.国际化 会使用正则表达式 ...
- 在用easyui中做CRUD功能时,当删除一行或多行数据后再点击修改会提示你选中了多行,如何解决这个bug了?
在用easyui中做CRUD功能时,当删除一行或多行数据后再点击修改会提示你选中了多行,如何解决这个bug了? 在删除成功后,加上这句话就可以了:$("#dg").datagrid ...