zoukankan      html  css  js  c++  java
  • Getting Started with the C# Driver

    1.下载

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

    如果安装的.msi文件,它会将C#驱动DLL放在C:Program Files (x86)MongoDBCSharp 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);
            }
        }
    }

     

     

     

     

     

查看全文
  • 相关阅读:
    leetcode 190 Reverse Bits
    vs2010 单文档MFC 通过加载位图文件作为客户区背景
    leetcode 198 House Robber
    记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
    逆序数2 HDOJ 1394 Minimum Inversion Number
    矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence
    递推DP URAL 1586 Threeprime Numbers
    递推DP URAL 1167 Bicolored Horses
    递推DP URAL 1017 Staircases
    01背包 URAL 1073 Square Country
  • 原文地址:https://www.cnblogs.com/msdynax/p/3740078.html
  • Copyright © 2011-2022 走看看