?
晚上閑來無事,研究了一下最近很火的MongoDB。感受只有一個:“自由”。
閑話略過,先讓MongoDB在Win7 64下跑起來:
1. MongoDB主頁
2. 下載
http://downloads.mongodb.org/win32/mongodb-win32-x86_64-1.8.1.zip
3. 解壓
e.g. H:\mongodb-win32-x86_64-1.8.1
4. 準備目錄
e.g. H:\mongo\data --保存數據庫文件 H:\mongo\logs --保存log,在此目錄下手動創建一個log.txt
5. 安裝
Win鍵,輸入cmd,右鍵-Run as Administrator
cd H:\mongodb-win32-x86_64-1.8.1\bin
H:
mongod --bind_ip 127.0.0.1 --logpath H:\mongo\logs\log.txt --logappend --dbpath H:\mongo\data --directoryperdb –install
net start “MongoDB”
成功。
6. 測試安裝
在之前的CMD窗口中輸入:mongo
應該能無異常進入query界面
輸入一個3+3,應該能得到6
關掉cmd,這玩意兒沒用了。
7. 下載.Net Connector
https://github.com/mongodb/mongo-csharp-driver/archives/master
解壓
打開CSharpDriverSetup-2010.sln
Build
失敗
刪除DriverSetup中對CHM的引用
重新編譯,成功
右鍵DriverSetup – Install
下一步下一步下一步。。。
8. 創建Asp.Net MVC3 Empty project
若無,請自行安裝MVC3的TOOL
9. Add ConnectionString in web.config
???? < add? name ="MongoDB" ?connectionString ="mongodb://localhost/test" />
?? </ connectionStrings >
10. Add Controller
?
Controller with empty read/write actions
11. Access to the DB
????????{
???????????? return ?MongoDatabase.Create(ConfigurationManager.ConnectionStrings[ " MongoDB " ].ConnectionString);
? ? ? ? }?
12. Create Action
?????????[HttpPost]
????????{
???????????? try
????????????{
????????????????var?db? = ?GetDB();
???????????????? foreach ?(var?key? in ?collection.AllKeys)
????????????????{
????????????????????db[ " testTable " ].Insert( new ?MongoDB.Bson.BsonDocument{
???????????????????????{key,?collection[key]}
????????????????????});
????????????????}
???????????????? return ?RedirectToAction( " Index " );
????????????}
???????????? catch
????????????{
???????????????? return ?View();
????????????}
????????}
13. Index Action
????????{
???????????? try
????????????{
????????????????var?db? = ?GetDB();
????????????????var?testTable? = ?db[ " testTable " ].FindAll();
????????????????var?result? = ? new ?StringBuilder();
???????????????? foreach ?(var?testData? in ?testTable)
????????????????{
???????????????????? foreach ?(var?property? in ?testData.Names)
????????????????????{
????????????????????????result.AppendFormat( " {0}:{1}? " ,?property,?testData[property]);
????????????????????}
????????????????????result.Append( " <br?/> " );
????????????????}
???????????????? return ?Content(result.ToString());
????????????}
???????????? catch
????????????{
???????????????? return ?View();
????????????}
????????}
14. Create Views under Views/Home/
Create.cshtml:
????ViewBag.Title?=?"Create";
????Layout?=?"~/Views/Shared/_Layout.cshtml";
}
< h2 > Create </ h2 >
< form? method ="post" ?action ="/Home/Create" >
????Name:
???? < input? name ="name" ?type ="text" ? />< br? />
????Age:
???? < input? name ="age" ?type ="text" ? />< br? />
????Gender:
???? < input? name ="gender" ?type ="text" ? />< br? />
????Married:
???? < input? name ="married" ?type ="text" ? />< br? />
???? < input? type ="submit" ?value ="Add" ? />
</ form >
Index.cshtml
????ViewBag.Title?=?"Index";
????Layout?=?"~/Views/Shared/_Layout.cshtml";
}
< h2 > Index </ h2 >
< a? href ="Create" > Create?New </ a >
15. Modify Global.asax.cs
???????????????? " Default " ,? // ?Route?name
???????????????? " {controller}/{action}/{id} " ,? // ?URL?with?parameters
???????????????? new ?{?controller? = ? " Home " ,?action? = ? " Create " ,?id? = ?UrlParameter.Optional?}? // ?Parameter?defaults
????????????);
11. Build & Run
?
Press Add
?
All Done. Happy and enjoy.
?
補上項目下載:
http://files.cnblogs.com/pandora/MvcApplication1.zip?
當前標簽: MongoDB
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
