學習mongodb,試着翻譯寫,英語能力有限,但願你們指正,不暢地方你們擔待,會後續翻譯後面內容;git
本介紹提供了足夠的信息,讓你開始使用C#的驅動程序。起步以後,你能夠參考文檔的其他部分,瞭解更多信息。github
最簡單的下載C#官方驅動的方式是經過Nuget,(當前version:1.8.2)mongodb
你也能夠在此地址下載該驅動數據庫
https://github.com/mongodb/mongo-csharp-driver/releases安全
若是是下載的是.zip文件,只要簡單的解壓並放在任何一個位置便可,若是下載的是.msi文件,點擊便可運行安裝,該程序會把全部dlls安裝到C:\ProgramFiles (x86)\MongoDB\CSharp Driver 1.x服務器
具體的路徑根據你的系統而定函數
右鍵點擊添加引用找到相應的dll添加到解決方案中,你須要添加如下dll的引用:學習
固然,你能夠直接用NuGet直接添加C#驅動dll到你的解決方案中。ui
至少你要引入如下命名空間spa
using MongoDB.Bson; using MongoDB.Driver;
另外,你可能會常常引入下面一個或者多個命名空間
1 using MongoDB.Driver.Builders; 2 using MongoDB.Driver.GridFS; 3 using MongoDB.Driver.Linq;
在其餘特殊的一些狀況下,會要引入另外的一些命名空間
最簡單的獲取client object引用的方式是經過國一個鏈接字符串
1 String connectionString = "mongodb://localhost"; 2 MongoClient client = new MongoClient(connectionString);
你能夠存儲client object 在一個全局變量中,MongoClient是線程安全的。
Server object對象經過client object對象來建立::
1 MongoServer server = client.GetServer();
經過server object對象獲取數據庫對象引用
1 var database = server.GetDatabase("test"); // test爲adb名稱
若是你使用的不值一個數據庫,你能夠經過GetDatabase獲取任何一個數據庫對象的引用
有兩種讓你能夠建立document方式:
若是數據很難或者很散很難定義成相關實體,你能夠經過BsonDocument來操做。
因爲使用自定義類比較方便,這個教程中將要使用這種方式,C#驅動程序可讓使用你提供了一下內容的自定義類
1.具備無參的構造函數
2.定義了可讀寫的屬性
這些要求和.NET’s XmlSerializer.所要求的同樣
另外,若是你的自定義類做爲根文檔,它必須包含一個id字段或者一個id屬性,(可是須要時你能夠重寫他),通常id類型爲ObjectId,但並未對其類型進行強制約束
請看下面類實體定義:
public class Entity { public ObjectId Id { get; set; } public string Name { get; set; } }
你能夠經過以下方式獲取集合對象
// "entities" 爲集合名稱 var collection = database.GetCollection<Entity>("entities");
插入一個實體對象:
var entity = new Entity { Name = "Tom" }; collection.Insert(entity); var id = entity.Id; // insert時候會對id進行賦值
在這個例子中假設知道id的值,咱們將讀取這個實體對象的值
var query = Query<Entity>.EQ(e => e.Id, id); var entity = collection.FindOne(query);
Query<Entity>.EQ 使用Query泛型類建立query對象,lambda表達式e => e.Id 指向到咱們集合中定義的字段
說明:
通常說來,數據庫中字段是和自定義實體中字段徹底同樣的,可是id是一個例外,他會映射到數據庫中_id字段
其餘查詢操做
包括: GT, 大於
GTE,大於等於
In, LT, LTE, Near, NE, And, Or還有一些其餘的
你能夠像這樣保存一個文檔:
entity.Name = "Dick"; collection.Save(entity);
保存的另外一個方式就是更新,不一樣之處在於保存會發送整個對象到服務器,更新只會發送對象改變的部分,例如:
var query = Query<Entity>.EQ(e => e.Id, id); var update = Update<Entity>.Set(e => e.Name, "Harry"); //更新 collection.Update(query, update);
經過update方法方便的進行更新操做
你能夠經過下面的方式移除文檔對象
var query = Query<Entity>.EQ(e => e.Id, id); collection.Remove(query);
C#的驅動程序都有一個鏈接池,有效地使用鏈接到服務器。因此無需調用Connect 或者Disconnect;只要交給驅動處理便可(調用Connect 沒什麼影響,可是調用Disconnect 會關掉鏈接池全部鏈接)
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); } } }