MongoDB 之C#实践
官方驱动:https://github.com/mongodb/mongo-csharp-driver/downloads。下载后,还提供了一个酷似msdn的帮助文档。
samus驱动:https://github.com/samus/mongodb-csharp/downloads。
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Builders; namespace TestMongoDb
{
class Program
{
public static string connectionString = "mongodb://localhost";
//数据库名
private static string databaseName = "TestDb"; static void Main(string[] args)
{
// 添加一条数据
//Users us = new Users() { Age = 123, Name = "TestNameA", Sex = "F" };
//us.Insert(); // 删除一条数据
//IMongoQuery iq = new QueryDocument("name", "TestNameA");
//Users.Remove(iq); //修改一条数据
//IMongoQuery iq = new QueryDocument("name", "TestNameA");
//IMongoUpdate iu = MongoDB.Driver.Builders.Update.Set("sex", "M").Set("age", 100);
//Users.Update(iq, iu); //获取数据列表
//IMongoQuery iq = new QueryDocument("name", "TestNameA");
IMongoQuery iq = Query.And(Query.GTE("age", ), Query.Matches("name", "/^Test/"));//>40
List<Users> userList = Users.Search(iq).ToList(); foreach (Users item in userList)
{
Console.WriteLine(item.Name + " " + item.Sex);
}
} public class Users
{
private static string tableUser = "Users"; public Users() { }
public Users(String name, Int32 age, String sex)
{
Name = name;
Age = age;
Sex = sex;
}
public String Name { get; set; }
public Int32 Age { get; set; }
public String Sex { get; set; }
public Boolean Insert()
{
BsonDocument dom = new BsonDocument {
{ "name", Name },
{ "age", Age },
{"sex",Sex}
};
return MongoHelper.Insert(tableUser, dom);
}
public static IEnumerable<Users> Search(IMongoQuery query)
{
foreach (BsonDocument tmp in MongoHelper.Search(tableUser, query))
yield return new Users(tmp["name"].AsString, tmp["age"].AsInt32, tmp["sex"].AsString);
}
public static Boolean Remove(IMongoQuery query)
{
return MongoHelper.Remove(tableUser, query);
}
public static Boolean Update(IMongoQuery query, IMongoUpdate new_doc)
{
return MongoHelper.Update(tableUser, query, new_doc);
}
}
public static class MongoHelper
{
public static MongoCursor<BsonDocument> Search(String collectionName, IMongoQuery query)
{
//定义Mongo服务
MongoServer server = MongoServer.Create(connectionString);
//获取databaseName对应的数据库,不存在则自动创建
MongoDatabase mongoDatabase = server.GetDatabase(databaseName);
MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName);
try
{
if (query == null)
return collection.FindAll();
else
return collection.Find(query);
}
finally
{
server.Disconnect();
}
}
/// <summary>
/// 新增
/// </summary>
public static Boolean Insert(String collectionName, BsonDocument document)
{
MongoServer server = MongoServer.Create(connectionString);
MongoDatabase mongoDatabase = server.GetDatabase(databaseName);
MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName);
try
{
collection.Insert(document);
server.Disconnect();
return true;
}
catch
{
server.Disconnect();
return false;
}
}
/// <summary>
/// 修改
/// </summary>
public static Boolean Update(String collectionName, IMongoQuery query, IMongoUpdate new_doc)
{
MongoServer server = MongoServer.Create(connectionString);
MongoDatabase mongoDatabase = server.GetDatabase(databaseName);
MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName);
try
{
collection.Update(query, new_doc);
server.Disconnect();
return true;
}
catch
{
server.Disconnect();
return false;
}
}
/// <summary>
/// 移除
/// </summary>
public static Boolean Remove(String collectionName, IMongoQuery query)
{
MongoServer server = MongoServer.Create(connectionString);
MongoDatabase mongoDatabase = server.GetDatabase(databaseName);
MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName);
try
{
collection.Remove(query);
server.Disconnect();
return true;
}
catch
{
server.Disconnect();
return false;
}
}
}
}
}
MongoDB 之C#实践的更多相关文章
- Mongodb极简实践
MongoDB 极简实践入门 1. 为什么用MongoDB? 传统的计算机应用大多使用关系型数据库来存储数据,比如大家可能熟悉的MySql, Sqlite等等,它的特点是数据以表格(table)的形式 ...
- MongoDB 极简实践入门
原作者StevenSLXie; 原链接(https://github.com/StevenSLXie/Tutorials-for-Web-Developers/blob/master/MongoDB% ...
- MongoDB开发最佳实践
MongoDB开发最佳实践 连接到MongoDB · 关于驱动程序:总是选择与所用之MongoDB相兼容的驱动程序.这可以很容易地从驱动兼容对照表中查到: · 如果使用第三方框架(如Spring Da ...
- java mongodb连接配置实践——生产环境最优
之前百度,google了很多,发现并没有介绍mongodb生产环境如何配置的文章, 当时想参考下都不行, 所以写篇文章,大家可以一块讨论下. 1. MongoClientOptions中的连接池配置: ...
- mongodb连接配置实践
之前百度,google了很多,发现并没有介绍mongodb生产环境如何配置的文章, 当时想参考下都不行, 所以写篇文章,大家可以一块讨论下. 1. MongoClientOptions中的连接池配置: ...
- MongoDB 上手开发实践(入门上手开发这一篇就够了)
前言 MongoDB是一个介于 关系数据库 和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.它支持的数据结构非常松散,是类似 json 的 bson 格式,因此可以存储比较复 ...
- MongoDB最佳安全实践
在前文[15分钟从零开始搭建支持10w+用户的生产环境(二)]中提了一句MongoDB的安全,有小伙伴留心了,在公众号后台问.所以今天专门开个文,写一下关于MongoDB的安全. 一.我的一次Mong ...
- 如何将Log4Net 日志保存到mongodb数据库之实践
log4net的大名早有耳闻,一直没真正用过,这次开发APP项目准备在服务端使用log4net. 日志的数据量较大,频繁的写数据库容易影响系统整体性能,所以独立将日志写到mongodb数据库是不错的选 ...
- 暑假第七周总结(安装MongoDB和Tomcat以及MongoDB进行编程实践)
本周主要对MongoDB和Tomcat进行了安装,两项安装都遇到了一些问题.其中在对MongoDB安装过程中出现了什么没有秘钥安全证书的,最终找了一堆教程重复了好多遍之后安装成功,虽然在启动和关闭的时 ...
随机推荐
- VBA实例收集
1.工作表事件:固定制定区域激活,使之不能选择其他区域. Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target. ...
- java常用设计模式
一个程序员对设计模式的理解: "不懂"为什么要把很简单的东西搞得那么复杂. 后来随着软件开发经验的增加才开始明白我所看到的"复杂"恰恰就是设计模式的精髓所在,我 ...
- java方法重载(overload)、重写(override);this、super关键简介
一.方法重载: 条件:必须在一个类中,方法名称相同,参数列表不同(包括:数据类型.顺序.个数),典型案例构 造方重载. 注意:与返回值无关 二.方法重写: 条件: (1)继承某个类或实现某接口 (2 ...
- 常用CSS缩写语法总结
使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.css缩写的主要规则如下: 颜色 16进制的色彩值,如果每两位的值相同,可以缩写一半,例如:#000000可以缩写为#000;#336699可以缩 ...
- js中的日期控件My97 DatePicker---那些打酱油的日子
使用WdatePicker插件来渲染日期类型的页面. 以下代码用到的属性有: isShowClear是否显示清空按钮 skin皮肤的样式 readOnly是否只读 maxDate:最大的选择时间 &l ...
- 【原】iOS学习之UITabBar的隐藏
当页面使用 UITabBarController + UINavigationController 框架的时候,当跳转到详情页面的时候,如果 UITabBar 仍然存在的话就会造成逻辑混乱,用户体验也 ...
- Where product development should start
We all need to know our customers in order to create products they’ll actually buy. This is why the ...
- C#编写windows服务
项目要求: 数据库用有一张表,存放待下载文件的地址,服务需要轮训表将未下载的文件下载下来. 表结构如下: 过程: VS--文件-->新建项目-->windows-->windows服 ...
- swiper的初步使用
1.引入文件,顺序引入(此处基于jquery,且版本至少1.7以上) <link rel="stylesheet" href="path/to/swiper-3.4 ...
- vue.js的基本操作
1.{{message}}输出data数据中的message. 2.v-for="todo in todos"输出data数据中的dotos数组 3.v-on:click=&quo ...