MongoDb C# 驱动操作示例
c#操作mongo数据库
驱动采用http://www.oschina.net/p/mongo-csharp-driver
public class Person
{
public ObjectId _id; public string Name { get; set; } public int Age { get; set; } public override string ToString()
{
return string.Format("id:{0} Name:{1} Age:{2}", _id, Name, Age);
}
}
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
//连接数据库字符串
string connectionStr = "mongodb://localhost";
MongoClient client = new MongoClient(connectionStr);
MongoServer server = client.GetServer();
//选择数据库
MongoDatabase db = server.GetDatabase("person");
//选择文档集合
MongoCollection<BsonDocument> collection = db.GetCollection("Person");
//插入数据*******************************************************************
//方法1:
BsonDocument person = new BsonDocument();
person.Add("Name", "test");
person.Add("Age", 10);
collection.Insert(person); //方法2:
for (int i = 0; i < 100; i++)
{
var perSon = new Person()
{
Name = "test" + i,
Age = i
};
collection.Insert(perSon);
}
//查询数据*******************************************************************
//小于20
QueryDocument queryD1 = new QueryDocument("Age", new QueryDocument("$lt", 20)); //
foreach (var perSon in collection.Find(queryD1))
{
Console.WriteLine(perSon);
}
//等于 xq20
QueryDocument queryD2 = new QueryDocument("Name", "test20"); //
foreach (var perSon in collection.Find(queryD2))
{
Console.WriteLine(perSon);
}
//等于 xq20
var query1 = Query.And(Query.EQ("Name","test20")); //
foreach (var perSon in collection.Find(query1))
{
Console.WriteLine(perSon);
}
linq方式:
//Linq查询
var query2 = collection.AsQueryable<Person>().Where(n => n.Name.Contains("test")).Take(20).ToList();
//.Where(n => n.Name == "xixihaha").ToList();
foreach (var per in query2)
{
Console.WriteLine(per);
}
//保存数据*******************************************************************
//Save1方法
var per = collection.AsQueryable<Person>().First(n => n.Name == "xixihaha");
//修改保存数据
per.Age = 50;
collection.Save(per);
per = collection.AsQueryable<Person>().First(n => n.Name == "xixihaha");
Console.WriteLine(per);
//Save2方法
var query = Query.And(Query.EQ("Name", "test5"));
var document = collection.FindOne(query);
if (document != null)
{
document["Age"] = 34;
collection.Save(document);
}
var per = collection.AsQueryable<Person>().First(n => n.Name == "test5");
Console.WriteLine(per);
//Update方法
var query = Query.And(Query.EQ("Name", "test5"));
var update = Update.Set("Age", 45);
collection.Update(query, update);
var per = collection.AsQueryable<Person>().First(n => n.Name == "test5");
Console.WriteLine(per);
////删除数据*******************************************************************
//删除指定文档
var query = Query.And(Query.EQ("Name", "test5"));
//删除所有文档
collection.RemoveAll();
源码地址:链接:http://pan.baidu.com/s/1b2OGGY 密码:cjp4
引用文档:http://www.cnblogs.com/wilber2013/p/4175825.html
MongoDb C# 驱动操作示例的更多相关文章
- MongoDB基础入门003--使用官方驱动操作mongo,C#
本篇先简单介绍一下,使用官方驱动来操作MongoDB.至于MongoDB原生的增删改查语句,且等以后再慢慢学习. 一.操作MongoDB的驱动主要有两个 1.官方驱动:https://github.c ...
- MongoDB学习比较-07 C#驱动操作MongoDB
下载驱动 驱动的下载有两种方式:一种是在C#项目中通过NuGet进行安装,另一种是通过下面的链接:https://github.com/mongodb/mongo-csharp-driver/rele ...
- 在C#中使用官方驱动操作MongoDB
MongoDB的官方驱动下载地址:https://github.com/mongodb/mongo-csharp-driver/releases 目前最新的版本是2.10,支持.NET 4.5以上.由 ...
- [转]MongoDB学习 C#驱动操作MongoDB
下载驱动 驱动的下载有两种方式:一种是在C#项目中通过NuGet进行安装,另一种是通过下面的链接:https://github.com/mongodb/mongo-csharp-driver/rele ...
- C#中使用官方驱动操作MongoDB
想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动有很多种,如官方提供的,samus. 实现思路大都类似.这里我们先用官方提供的mongo-csharp-dri ...
- [转载]在C#中使用官方驱动操作MongoDB
在C#中使用官方驱动操作MongoDB 8.1)下载安装 想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动有很多种,如官方提供的,samus. 实现思路大都类 ...
- MongoDB学习-->命令行增删改查&JAVA驱动操作Mongodb
MongoDB 是一个基于分布式文件存储的数据库. 由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关 ...
- 快速掌握mongoDB(五)——通过mongofiles和C#驱动操作GridFS
1 GridFS简介 当前Bson能存储的最大尺寸是16M,我们想把大于16M的文件存入mongoDB中怎么办呢?mongoDB提供的GridFS就是专门做这个的.使用GridFS存储大文件时,文件被 ...
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
随机推荐
- Vue处理ajax请求
Ajax请求 1>解决跨域问题 1.1前端解决.只需要在vue.config.js中增加devServer节点增加代理: const path = require("path" ...
- [已解决] odoo12 菜单不显示,安装后多出菜单
描述:odoo11中自定义模块写的,除了res.partner,res.users使用odoo自带的.其他的写了一个中国城市l10n_cn_city模型,一个账单模型(继承l10n_cn_city). ...
- message() 信息提示
//样式部分 .message { position: fixed;top: -100px;width: 400px;left: 50%;margin-left: -200px;z-index: 10 ...
- IIS7.0/8.0的错误HTTP Error 500.19 - Internal Server Error ,错误代码为0x80070021
最近在部署项目的时候,总是出现了这个问题. 大概原因为IIS7.0的安全设定相比前版本有很大的变更.IIS7.0的安全设置文件在%windir%\system32\inetsrv \config\ap ...
- python爬虫基础14-selenium大全8/8-常见问题
Selenium笔记(8)常见的坑 本文集链接:https://www.jianshu.com/nb/25338984 用Xpath查找数据时无法直接获取节点属性 通常在我们使用xpath时,可以使用 ...
- pyqt设计
pyqt是python设计GUI的第三方包 作为一个小白,我觉得这篇博客贼好,我就是按照这个博客写的. 这个博客一共分5步,每一步都特别详细. pyqt 打包exe时遇到的问题(我的python环境是 ...
- 谈谈你对Hibernate的理解
答: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管程序运行的过程和操作怎么样,本质上都是要得到一 ...
- python并发编程之进程2(管道,事件,信号量,进程池)
管道 Conn1,conn2 = Pipe() Conn1.recv() Conn1.send() 数据接收一次就没有了 from multiprocessing import Process,Pip ...
- 学习pwn的一些指导
使用ret2libc攻击方法绕过数据执行保护 http://blog.csdn.net/linyt/article/details/43643499 格式化字符串利用小结 http://www.cnb ...
- POJ:1330-Nearest Common Ancestors(LCA在线、离线、优化算法)
传送门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K ...