MongoDB CRUD

摘要:MongoDB CRUD

這次課題剛好挑到 MongoDB 害我連想成芒果 蒙古 傻傻分不清

在谷哥幫忙下找到MSDN發的文章(程式設計師雜談-使用MongoDB 投入NoSQL 的懷抱)

看完內部範例只有簡單SU,當然看完就要動手開工實作了!! 

那文章提到每個一個都要加入MongoDB.Driver參考http://github.com/samus/mongodb-csharp

 測試環境WIN7 SV2010  Mongo 1.4.3內部設定依照MSDN內文教學設定

那目前小弟也在努力測試中~希望有經驗大大指點

 

查詢--目前只依照範例去做簡單配置

01 using MongoDB.Driver;
02
03 public partial class _Default : System.Web.UI.Page  
04 {
05     protected void Page_Load(object sender, EventArgs e)
06     {  
07         Mongo db = new Mongo();
08         db.Connect();// 連接伺服器.跑Mongo預設port
09         Database test = db.GetDatabase("test");// 獲取資料庫
10         IMongoCollection things = test.GetCollection("things");// 獲取欄位集合
11         Document queryDoc = new Document();
12      /*已知oid 可以直接下編號查詢 */
13      // queryDoc.Append("_id", new MongoDB.Driver.Oid("4c0b25ba344a000000001d16"));
14         queryDoc.Append("lastname", "Lin");
15         Document resultDoc = things.FindOne(queryDoc);
16         Response.Write(resultDoc);
17         db.Disconnect();
18       
19         // 查询:
20         //Document doc = new Document();
21         //doc.Add("firstname", "joe");  //單個欄位
22         //doc.Add("Age", new Document().Append("$gte", 30).Append("$lte", 40));//多個欄位AND
23         // 讀取:
24         //table.FindOne(new Document());        
25     }

26 }

 

 

新增

 

01 using MongoDB.Linq;
02 public partial class insert : System.Web.UI.Page
03 {
04     
05     protected void Page_Load(object sender, EventArgs e)
06     {
07
08     }

09     protected void Button1_Click(object sender, EventArgs e)
10     {
11         Mongo db = new Mongo();
12         db.Connect(); // 連接伺服器
13         Database test = db.GetDatabase("test");// 獲取資料庫
14         IMongoCollection things = test.GetCollection("things"); ; // 獲取集合
15         Document anotherResult=new Document();  
16         anotherResult["firstname"] = TextBox1.Text;
17         anotherResult["lastnaem"] = TextBox2.Text;
18         anotherResult["age"] = int.Parse(TextBox3.Text);
19         things.Insert(anotherResult); ; // 插入
20         Response.Write("新增成功");            
21         db.Dispose();
22         db.Disconnect();
23     }

24 }

更新

 

01 using MongoDB.Driver;
02 public partial class update : System.Web.UI.Page
03 {
04     IMongoCollection things;
05     protected void Page_Load(object sender, EventArgs e)
06     {
07         Mongo db = new Mongo();
08         db.Connect();  
09
10         //Database test = db.GetDatabase("test");
11         //things = test.GetCollection("things");
12         //Document queryDoc = new Document();
13         //queryDoc.Append("fisrtname" , "chi-hsuan");
14         //Document resultDoc = things.FindOne(queryDoc);
15         //Response.Write(resultDoc +"</p>");
16         /*可縮寫成下面一行*/
17
18         Document anotherResult = db["test"]["things"].FindOne(new Document().Append("lastname", "Lin"));      
19         Response.Write(anotherResult + "</p>");
20         db.Dispose();
21         db.Disconnect();
22         
23         
24     }

25     protected void Button1_Click(object sender, EventArgs e)
26     {
27         Mongo db = new Mongo();
28         db.Connect();
29         Database test = db.GetDatabase("test");
30         things = test.GetCollection("things");
31         Document anotherResult =
32        db["test"]["things"].FindOne(new Document().Append("lastname", "Lin"));
33         anotherResult["age"] = int.Parse(TextBox1.Text);
34         things.Update(anotherResult);
35        // Response.Write(anotherResult);
36         Response.Redirect("update.aspx");
37         db.Dispose();
38         db.Disconnect();
39         //多個欄位更新
40         //Document ted = new Document();
41         //ted["_id"] = new MongoDB.Driver.Oid("4c0b25ba344a000000001d16");
42         //ted["firstname"] = "chi";
43         //ted["lastname"] = "Neward";
44         //ted["age"] = 40;
45         //things.Update(ted);      
46     }

47 }

刪除

01 using MongoDB.Driver;
02 public partial class update : System.Web.UI.Page
03 {
04     IMongoCollection things;
05     protected void Page_Load(object sender, EventArgs e)
06     {  
07     }

08     protected void Button1_Click(object sender, EventArgs e)
09     {    
10         Mongo db = new Mongo();
11         db.Connect();
12         Database test = db.GetDatabase("test");
13         things = test.GetCollection("things");
14         Document anotherResult =
15         db["test"]["things"].FindOne(new Document().Append("age", int.Parse(TextBox1.Text)));
16        // anotherResult["age"] = int.Parse(TextBox1.Text);
17         things.Delete(anotherResult);
18         Response.Redirect("del.aspx");
19         db.Dispose();
20         db.Disconnect();
21     }

22 }

 

目前卡在怎麼下select * from 雖然資料庫內下指令很簡單db.things.find()

但要怎麼轉成已經寫好的class  function內呢?

還有count又該如何用~真多疑問阿~@@

大家一起起來努力切磋吧!!

 

 

 

 

大家一起加入blogads 賺零用錢!!