傳統的關系數據庫一般由數據庫(database)、表(table)、記錄(record)三個層次概念組成,MongoDB是由數據庫(database)、集合(collection)、文檔對象(document)三個層次組成。MongoDB對於關系型數據庫裡的表,但是集合中沒有列、行和關系概念,這體現了模式自由的特點。
在C#中想使用MongoDB我們還需要下載驅動,現在一般比較常見的驅動有官網驅動,samus驅動。samus驅動除了支持一般形式的操作之外,還支持linq方式操縱數據。本文所使用的為官網驅動。官網驅動地址:點擊下載
下載完後我們在下載包中可以得到二個DLL:
MongoDB.Driver.dll 驅動程序
MongoDB.Bson.dll 序列化、Json相關
新增:
//數據庫連接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //數據庫 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //新增 student stud_Add = new student() { name="趙雲", code="2001", sex="女", age="23" }; collection.Insert<student>(stud_Add); //查詢 QueryDocument query_sel = new QueryDocument { { "name", "趙雲" } }; var list = collection.FindAs<student>(query_sel); foreach (student item in list) { Console.WriteLine("name:{0},code:{1}", item.name, item.code); }
顯示結果:
修改:
//數據庫連接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //數據庫 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //更新 QueryDocument query_upd = new QueryDocument { { "name", "趙雲" } }; IMongoUpdate update = Update.Set("code", "4444"); collection.Update(query_upd, update);
顯示結果:
刪除:
//數據庫連接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //數據庫 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //刪除 QueryDocument query_del = new QueryDocument { { "name", "趙雲" } }; collection.Remove(query_del);
全量代碼:
static void Main(string[] args) { //數據庫連接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //數據庫 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //新增 student stud_Add = new student() { name="趙雲", code="2001", sex="女", age="23" }; collection.Insert<student>(stud_Add); //更新 QueryDocument query_upd = new QueryDocument { { "name", "趙雲" } }; IMongoUpdate update = Update.Set("code", "4444"); collection.Update(query_upd, update); //刪除 QueryDocument query_del = new QueryDocument { { "name", "趙雲" } }; collection.Remove(query_del); //查詢 QueryDocument query_sel = new QueryDocument { { "name", "趙雲" } }; var list = collection.FindAs<student>(query_sel); foreach (student item in list) { Console.WriteLine("name:{0},code:{1}", item.name, item.code); } } public class student { public ObjectId _id; //BsonType.ObjectId 這個對應了 MongoDB.Bson.ObjectId public string name { get; set; } public string code { get; set; } public string sex { get; set; } public string age { get; set; } /*_id 屬性必須要有,否則在更新數據時會報錯:“Element '_id' does not match any field or property of class”。*/ } View Code基礎的增刪改查就是上面所述。
實例代碼:點擊下載