在c#中使用mongo-csharp-driver操作mongodb
1)下载安装
下载地址:http://github.com/mongodb/mongo-csharp-driver/downloads
编译之后得到两个dll
MongoDB.Driver.dll:顾名思义,驱动程序
MongoDB.Bson.dll:序列化、Json相关
引用这两个dll。
2)连接数据库:
在连接数据库之前请先确认您的MongoDB已经开启了。
//数据库连接字符串
const string strconn = "mongodb://127.0.0.1:27017";
//数据库名称
const string dbName = "testdb";
//创建数据库链接
MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
//获得数据库test
MongoDatabase db = server.GetDatabase(dbName);
3)插入数据:
好了数据打开了,现在得添加数据了,我们要添加一条User“记录”到 Users集合中。
在MongoDB中没有表的概念,所以在插入数据之前不需要创建表。
但我们得定义好要插入的数据的模型Users
Users.cs:
public class Users
{
public ObjectId _id;//BsonType.ObjectId 这个对应了 MongoDB.Bson.ObjectId
public string Name { get; set; }
public string Sex { set; get; }
}
_id 属性必须要有,否则在更新数据时会报错:“Element '_id' does not match any field or property of class”。
添加数据的代码:
public void Insert()
{
//创建数据库链接
MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
//获得数据库test
MongoDatabase db = server.GetDatabase(dbName);
Users users = new Users();
users.Name = "test";
users.Sex = "man";
//获得Users集合,如果数据库中没有,先新建一个
MongoCollection col = db.GetCollection("Users");
//执行插入操作
col.Insert<Users>(users);
}
4)更新数据
public void Update()
{
//创建数据库链接
MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
//获得数据库test
MongoDatabase db = server.GetDatabase(dbName);
//获取Users集合
MongoCollection col = db.GetCollection("Users");
//定义获取“Name”值为“test”的查询条件
var query = new QueryDocument { { "Name", "test" } };
//定义更新文档
var update = new UpdateDocument { { "$set", new QueryDocument { { "Sex", "wowen" } } } };
//执行更新操作
col.Update(query, update);
}
5)删除数据
public void Delete()
{
//创建数据库链接
MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
//获得数据库test
MongoDatabase db = server.GetDatabase(dbName);
//获取Users集合
MongoCollection col = db.GetCollection("Users");
//定义获取“Name”值为“test”的查询条件
var query = new QueryDocument { { "Name", "test" } };
//执行删除操作
col.Remove(query);
}
6)查询数据
public void Query()
{
//创建数据库链接
MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
//获得数据库test
MongoDatabase db = server.GetDatabase(dbName);
//获取Users集合
MongoCollection col = db.GetCollection("Users");
//定义获取“Name”值为“test”的查询条件
var query = new QueryDocument { { "Name", "test" } };
//查询全部集合里的数据
var result1 = col.FindAllAs<Users>();
//查询指定查询条件的第一条数据,查询条件可缺省。
var result2 = col.FindOneAs<Users>();
//查询指定查询条件的全部数据
var result3 = col.FindAs<Users>(query);
}
在c#中使用mongo-csharp-driver操作mongodb的更多相关文章
- 在Thinkphp3.1中使用Mongo的具体操作
最近研究Mongo项目都是用TP开发的,先介绍下Mongo在TP3.1中的用法 首先要确保你的PHP环境中已经安装好Mongo扩展,在实际项目中大多数都是Mysql数据库为主的,那么如何添加一个Mon ...
- MongoDB学习-->命令行增删改查&JAVA驱动操作Mongodb
MongoDB 是一个基于分布式文件存储的数据库. 由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关 ...
- c#操作MangoDB 之MangoDB CSharp Driver驱动详解
序言 MangoDB CSharp Driver是c#操作mongodb的官方驱动. 官方Api文档:http://api.mongodb.org/csharp/2.2/html/R_Project_ ...
- [转载]在C#中使用官方驱动操作MongoDB
在C#中使用官方驱动操作MongoDB 8.1)下载安装 想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动有很多种,如官方提供的,samus. 实现思路大都类 ...
- C#中使用官方驱动操作MongoDB
想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动有很多种,如官方提供的,samus. 实现思路大都类似.这里我们先用官方提供的mongo-csharp-dri ...
- MangoDB CSharp Driver
1.引用MongoDB for C# Driver 从网上下载C#访问MongoDB的驱动,得到两个DLL: MongoDB.Driver.dll MongoDB.Bson.dll 将它们引用到项目中 ...
- php操作mongodb中的ISODate格式日期
mongodb 中数据记录的日期格式为"dateCreated" : ISODate("2011-12-20T07:22:50.836Z")经过翻阅php官网中 ...
- 使用Mongo官方驱动操作Mongo数据库
首先到 https://github.com/mongodb/mongo-csharp-driver/downloads 下载Mongo官方驱动 下载完成后引用到项目中 public class Co ...
- C# Asp.net中简单操作MongoDB数据库(一)
需要引用MongoDB.Driver.dll.MongoDB.Driver.core.dll.MongoDB.Bson.dll三个dll. 1.数据库连接: public class MongoDb ...
- Spring Boot中快速操作Mongodb
Spring Boot中快速操作Mongodb 在Spring Boot中集成Mongodb非常简单,只需要加入Mongodb的Starter包即可,代码如下: <dependency> ...
随机推荐
- struts2访问servlet API
搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...
- Eclipse CDT 代码高亮配置
效果图如下: 配置生效方式: 找到CDT的workspace目录中如下文件 X:\workspace\.metadata\.plugins\org.eclipse.core.runtime\.sett ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Redo与undo在开发中的使用
redo:记录用户的操作.(commit) undo:相反的用操作.(rollback) checkpoint:redo真正写入物理存储.(定时写---定时策略) ================== ...
- Oracle数据库——函数 http://www.jb51.net/article/40469.htm
1====分析函数 相当于把分组后的结果加到每一行里 SELECT t.loan_contract_no,t.loan_name,t.loan_amount,ROWNUM, row_number ...
- FZU Problem 2136 取糖果
Problem 2136 取糖果 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 有N个袋子放成一排,每个 ...
- strtol函数
今天做啦一个进制转换的题,改来改去最终倒是过啦,本来挺开心的,然后去翻啦一下题解,瞬间就有小情绪啦,哎,人家的代码辣么辣么短,实在是不开心,不过谁让咱是小渣渣呢,在此总结一下strtol 函数. 先来 ...
- spark streaming 实时计算
spark streaming 开发实例 本文将分以下几部分 spark 开发环境配置 如何创建spark项目 编写streaming代码示例 如何调试 环境配置: spark 原生语言是scala, ...
- java学习之(接口)
使用接口 接口不能用于创建实例,但接口可以用于声明引用类型变量.当使用接口来声明引用类型变量时,这个引用类型变量必须引用到其实现类的对象.除此之外,接口的主要用途就是被实现类实现.归纳起来,接口主要有 ...
- css的border效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...