MongoDB-Getting Started with the C# Driver

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   資料   

簡介:本文僅提供快速入門層級的使用C# Driver操作MongoDB,高手跳過

  1. Downloading the C# Driver
    1. 猛擊下載
  2. 添加相關的dll引用
        MongoDB.Bson.dll    MongoDB.Driver.dll
  3. 添加名稱空間引用
    using MongoDB.Bson;using MongoDB.Driver;
  4. 擷取用戶端對象
    var connectionString = "mongodb://localhost";var client = new MongoClient(connectionString);
  5. 擷取服務端對象
    var server = client.GetServer();
  6. 擷取要操作的資料庫
    var database = server.GetDatabase("test"); // "test" is the name of the database
  7. CRUD(使用自訂的類)
    1. 自訂實體
      public class Entity{    public ObjectId Id { get; set; }    public string Name { get; set; }}
    2. 擷取要操作的表
      // "entities" is the name of the collectionvar collection = database.GetCollection<Entity>("entities");
    3. 新增一條記錄
      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)
    4. 查詢一條記錄
      var query = Query<Entity>.EQ(e => e.Id, id);var entity = collection.FindOne(query);
    5. 儲存一條記錄(發送整個實體到資料庫)
      entity.Name = "Dick";collection.Save(entity);
    6. 修改一條記錄(僅發送修改的部分到資料庫,這一點是和儲存還是有區別的,根據情境來自行判斷需要用哪一種來更新資料)
      var query = Query<Entity>.EQ(e => e.Id, id);var update = Update<Entity>.Set(e => e.Name, "Harry"); // update modifierscollection.Update(query, update);
    7. 刪除一條記錄
      var query = Query<Entity>.EQ(e => e.Id, id);collection.Remove(query);

       

完整示範代碼:

 1 using MongoDB.Bson; 2 using MongoDB.Driver; 3 using MongoDB.Driver.Builders; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Linq.Expressions; 8 using System.Text; 9 using System.Threading.Tasks;10 11 namespace MongoDBTest12 {13     class Program14     {15         static void Main(string[] args)16         {17             var connectionString = "mongodb://localhost:27017";18             var client = new MongoClient(connectionString);19             var server = client.GetServer();20             var database = server.GetDatabase("test");21             var collection = database.GetCollection<Entity>("entities");22             var entity = new Entity { Name = "Tom" };23             var i = collection.Insert(entity);24             var id = entity.Id;25 26             var query = Query<Entity>.EQ(e => e.Id, id);27             entity = collection.FindOne(query);28             entity.Name = "Dick";29             var s = collection.Save(entity);30 31             var update = Update<Entity>.Set(e => e.Name, "Harry");32             collection.Update(query, update);33 34 35             collection.Remove(query);36 37             Console.ReadKey();38 39         }40 41 42     }43     public class Entity44     {45         public ObjectId Id;46         public string Name { get; set; }47     }48 49 }

 

MongoDB-Getting Started with the C# Driver

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.