惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
小众软件
小众软件
美团技术团队
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
D
Docker
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
雷峰网
雷峰网
The Cloudflare Blog

博客园 - Pandora

MongoDB on Asp.Net MVC3–CRUD [WebGL] 简介,流程及示例 2010 Web前端技术趋势及总结 转载 - 杂念0828 【F#2.0系列】定义递归函数 【F#2.0系列】使用选项(Option) 【F#2.0系列】使用F#的List 【F#2.0系列】介绍String类型 - Pandora - 博客园 【F#2.0系列】使用F#进行算术操作 【F#2.0系列】F#调用.NET 类库 【F#2.0系列】目录 【F#2.0系列】F#入门(2) 【F#2.0系列】F#入门(1) 【F#2.0系列】概述 活该如此 Visual Studio 2010 XAML Editor IntelliSense Extension Microsoft 发布了第一个IE9 developer preview 支持HTML5硬件加速 ASP.NET 4.0 来了 []()+! 实现JavaScript代码的原理
MongoDB on Asp.Net MVC3
Pandora · 2011-05-26 · via 博客园 - Pandora

晚上闲来无事,研究了一下最近很火的MongoDB。感受只有一个:“自由”。

闲话略过,先让MongoDB在Win7 64下跑起来:

1. MongoDB主页

http://www.mongodb.org/

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

  <connectionStrings>
    
<add name="MongoDB" connectionString="mongodb://localhost/test"/>

  </connectionStrings> 

10. Add Controller

Controller with empty read/write actions

11. Access to the DB

        private MongoDatabase GetDB()
        {
            
return MongoDatabase.Create(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);

        } 

12. Create Action

         [HttpPost]

        public ActionResult Create(FormCollection collection)
        {
            
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

        public ActionResult Index()
        {
            
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>
<href="Create">Create New</a>

15. Modify Global.asax.cs

            routes.MapRoute(
                
"Default"// Route name
                "{controller}/{action}/{id}"// URL with parameters
                new { controller = "Home", action = "Create", id = UrlParameter.Optional } // Parameter defaults
            );

11. Build & Run
image
 

Press Add

image

All Done. Happy and enjoy.

补上项目下载:

https://files.cnblogs.com/pandora/MvcApplication1.zip