傳統(tǒng)的關系數(shù)據(jù)庫一般由數(shù)據(jù)庫(database)、表(table)、記錄(record)三個層次概念組成,MongoDB是由數(shù)據(jù)庫(database)、集合(collection)、文檔對象(document)三個層次組成。MongoDB對于關系型數(shù)據(jù)庫里的表,但是集合中沒有列、行和關系概念,這體現(xiàn)了模式自由的特點。
在C#中想使用MongoDB我們還需要下載驅(qū)動,現(xiàn)在一般比較常見的驅(qū)動有官網(wǎng)驅(qū)動,samus驅(qū)動。samus驅(qū)動除了支持一般形式的操作之外,還支持linq方式操縱數(shù)據(jù)。本文所使用的為官網(wǎng)驅(qū)動。官網(wǎng)驅(qū)動地址:點擊下載
下載完后我們在下載包中可以得到二個DLL:
MongoDB.Driver.dll 驅(qū)動程序
MongoDB.Bson.dll 序列化、Json相關
新增:
//數(shù)據(jù)庫連接字符串const string connectionString = "mongodb://127.0.0.1:27017";//數(shù)據(jù)庫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);}顯示結(jié)果:

修改:
//數(shù)據(jù)庫連接字符串const string connectionString = "mongodb://127.0.0.1:27017";//數(shù)據(jù)庫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);顯示結(jié)果:

刪除:
//數(shù)據(jù)庫連接字符串const string connectionString = "mongodb://127.0.0.1:27017";//數(shù)據(jù)庫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){ //數(shù)據(jù)庫連接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //數(shù)據(jù)庫 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 屬性必須要有,否則在更新數(shù)據(jù)時會報錯:“Element '_id' does not match any field or PRoperty of class”。*/}View Code基礎的增刪改查就是上面所述。
實例代碼:點擊下載
新聞熱點
疑難解答