1.下载

如果下载的.zip文件,只需要解压即可。

如果安装的.msi文件,它会将C#驱动DLL放在C:\Program Files (x86)\MongoDB\CSharp Driver xxx的位置。

2.将C#驱动DLL添加引用

  • MongoDB.Bson.dll
  • MongoDB.Driver.dll
  • 你也可以使用NuGet包,给项目安装驱动。

    3.添加using声明

    using MongoDB.Bson;
    using MongoDB.Driver; using MongoDB.Driver.Builders;
    using MongoDB.Driver.GridFS;
    using MongoDB.Driver.Linq;

    4.获取一个Client对象的引用

    使用一个连接字符串,是活的一个Client对象的简单方式。

    var connectionString = "mongodb://localhost";
    var client = new MongoClient(connectionString);

    如果你要在全局变量中存储Client对象,你可以使用MongoClient,它是线程安安全。

    5.获取一个Server对象的引用

    var server = client.GetServer();

    6.获取一个Database对象的引用

    var database = server.GetDatabase("test"); // "test" is the name of the database

    如果你使用多个Database,可以为每个Database调用一次GetDatabase。

    7.BsonDocument Object Model vs. Your Own Domain Classes

    你有两种使用collections的方式

    • 使用BsonDocument object model
    • 使用你的own domain classes

    当你使用的数据,难以或不可能为它定义domain classes时,你应该使用BsonDocument object model。

    因为它和你的own domain classes一起工作是如此简单。C#驱动可以和你的domain classes一起工作,为他们提供:

    • 有一个没商量的构造器
    • 为你要存储在数据库中的字段,定义公共读/写属性

    这些装备,本质上和.NET的XmlSerializer一样。

    另外,如果你的domain class 要用于作为root document,它必须包含一个Id字段或属性(一般叫做Id,你也可以覆盖它)。一般Id会是ObjectId类型,但这里没有强制该成员的类型。

    考虑遵循下面的类定义:

    public class Entity
    {
    public ObjectId Id { get; set; } public string Name { get; set; }
    }

    8.获取一个Collection Object的引用

    像这样,你会得到一个包含Entity文档的集合:

    // "entities" is the name of the collection
    var collection = database.GetCollection<Entity>("entities");

    9.Insert a Document

    Insert一个Entity:

    var entity = new Entity { Name = "Tom" };
    collection.Insert(entity);
    var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)

    10.Find一个现存的Document

    假设我们已知Id的值,会读回一个Entity:

    var query = Query<Entity>.EQ(e => e.Id, id);
    var entity = collection.FindOne(query);

    Query<Entity>.EQ使用Query<T>构建类,来构建查询。Lambda表达式e=>e.Id,翻译成_id。这是该字段存储在数据库中的名字。

    注意:一般,字段在数据库中的名字,与它在domain class中的字段或属性名一样。但Id,是一个例外,它与数据库中的_id映射。

    其他查询操作,包括:GT,GTE,In,LT,LTE,Near,NE,And,Or(和其他很特殊的)。

    11.Save a Document

    你可以像下面这样,保存对现有document的改变。

    entity.Name = "Dick";
    collection.Save(entity);

    12.更新一个现有Document

    另一种Save,是Update。不同之处是,Save将实体文档发回服务器,而Update仅仅发送改变。例如:

    var query = Query<Entity>.EQ(e => e.Id, id);
    var update = Update<Entity>.Set(e => e.Name, "Harry");
    // update modifiers
    collection.Update(query, update);

    该例子使用Update<T>构建器,轻松构建update修改。

    13.Remove一个现存Document

    要从集合中移除一个现存document,你可以这么写:

    var query = Query<Entity>.EQ(e => e.Id, id);
    collection.Remove(query);

    14.你不需要调用Connect或Disconnect

    C#驱动有一个connection pool,用来高效地连接服务器。这样就不用调用Connect或Disconnect。就让驱动来关心连接吧(调用Connect是无害的,但调用Disconnect是非常糟糕。因为他关闭所有connection pool中的链接)。

    15.完整的示例程序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; using MongoDB.Bson;
    using MongoDB.Driver;
    using MongoDB.Driver.Builders; namespace ConsoleApplication1
    {
    public class Entity
    {
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    } class Program
    {
    static void Main(string[] args)
    {
    var connectionString = "mongodb://localhost";
    var client = new MongoClient(connectionString);
    var server = client.GetServer();
    var database = server.GetDatabase("test");
    var collection = database.GetCollection<Entity>("entities"); var entity = new Entity { Name = "Tom" };
    collection.Insert(entity);
    var id = entity.Id; var query = Query<Entity>.EQ(e => e.Id, id);
    entity = collection.FindOne(query); entity.Name = "Dick";
    collection.Save(entity); var update = Update<Entity>.Set(e => e.Name, "Harry");
    collection.Update(query, update); collection.Remove(query);
    }
    }
    }

    Getting Started with the C# Driver的更多相关文章

    1. 深入linux kernel内核配置选项

      ============================================================================== 深入linux kernel内核配置选项 ...

    2. MongoDB Java Driver操作指南

      MongoDB为Java提供了非常丰富的API操作,相比关系型数据库,这种NoSQL本身的数据也有点面向对象的意思,所以对于Java来说,Mongo的数据结构更加友好. MongoDB在今年做了一次重 ...

    3. c#操作MangoDB 之MangoDB CSharp Driver驱动详解

      序言 MangoDB CSharp Driver是c#操作mongodb的官方驱动. 官方Api文档:http://api.mongodb.org/csharp/2.2/html/R_Project_ ...

    4. Java JDBC Thin Driver 连接 Oracle 三种方法说明(转载)

      一.JDBC 连接Oracle 说明 JDBC 的应用连接Oracle 遇到问题,错误如下: ORA-12505,TNS:listener does not currently know of SID ...

    5. 设备模型(device-model)之平台总线(bus),驱动(driver),设备(device)

      关于关于驱动设备模型相关概念请参考<Linux Device Drivers>等相关书籍,和内核源码目录...\Documentation\driver-model 简单来说总线(bus) ...

    6. AM335x tscadc platform driver 相关代码跟踪

      TI AM335x ti am335x_tsc.c 代码跟踪 在kernel 首层目录: 先运行make ARCH=arm tags 这个作用是建立tags文件,只含有arm架构的,利用ctag即可进 ...

    7. selenium web driver 实现截图功能

      在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...

    8. selenium web driver 使用JS修改input属性

      selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改 首先html源文件如下,设置为text .hidden.submit ...

    9. 转载:安装ie driver和chrome driver

      很多同学在使用webdriver的时候总是忘了安装ie driver和chrome driver, 因此在这里简单介绍一下这2个driver的安装方式. IE driver 在新版本的webdrive ...

    10. U-Boot Driver Model领域模型设计

      需求分析 在2014年以前,uboot没有一种类似于linux kernel的设备驱动模型,随着uboot支持的设备越来越多,其一直受到如下问题困扰: 设备初始化流程都独立实现,而且为了集成到系统,需 ...

    随机推荐

    1. [BS-04] 在iOS中对系统定义的类的readonly属性可通过KVC进行赋值

      系统提供的类的readonly属性可通过KVC进行赋值 UITabBarController.h @interface UITabBarController : UIViewController &l ...

    2. error MIDL2311 : statements outside library block are illegal in mktyplib compatability mode

      解决办法: 1. Do not use the /mktyplib203 switch unless you have to deal with legacy code dating back to ...

    3. D3D9 GPU Hacks (转载)

      D3D9 GPU Hacks I’ve been trying to catch up what hacks GPU vendors have exposed in Direct3D9, and tu ...

    4. time模块学习

      时间三种形式: 1.timestamp   从1970-1-1 00:00到现在经历的秒数 2.string_time   Sat Mar 28 22:24:24 2009 3.struct_time ...

    5. docker offical docs:Working with Containers

      enough ---------------------------------------------------------------------------------- Working wi ...

    6. RadioButton 组,ComboBox用法:

      RadioButton 组 final ToggleGroup group = new ToggleGroup(); final RadioButton rb1 = new RadioButton(& ...

    7. Redis:安装

      此篇文章并不介绍linux下安装方法.围绕windows安装而介绍 两种安装方式: 1)下载压缩包,解压后(运行起来有个窗口,关闭掉就不在运行),没有windows服务被注册:可以只用命令在cmd将其 ...

    8. Swift游戏实战-跑酷熊猫 07 平台的移动

      这节内容我们来实现平台是怎么产生移动动画的. 要点 1 利用数组存放平台 var platforms=[Platform]() 2 有新的平台产生存放进数组 platforms.append(plat ...

    9. Object-C 入门

      该文章转载自:http://sheng.iteye.com/blog/775588一:Objective-C入门 .Cocoa的组成 苹果公司将Cocoa.Carbon.QuickTime和OpenG ...

    10. maven的pom报plugins却是的解决方法(转)

      maven的pom报plugins却是的解决方法. 引用 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom: ...