Created
October 9, 2011 05:17
-
-
Save jasonmccay/1273340 to your computer and use it in GitHub Desktop.
Create your Developer Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Web.Mvc; | |
using System.Configuration; | |
using MongoDB.Driver; | |
using mongohq_csharp.Models; | |
using mongohq_csharp.Models.DTO; | |
namespace mongohq_csharp.Controllers | |
{ | |
public class DeveloperController : Controller | |
{ | |
readonly MongoDatabase mongo_db; | |
public DeveloperController() | |
{ | |
mongo_db = retreive_mongohq_db(); | |
} | |
public ActionResult Index() | |
{ | |
var model = new DeveloperModel(); | |
var developers_collection = mongo_db.GetCollection("developers").FindAll().AsEnumerable(); | |
model.developers = (from developer in developers_collection | |
select new DeveloperDTO | |
{ | |
id = developer["_id"].AsObjectId, | |
name = developer["name"].AsString, | |
languages = developer["languages"].AsBsonArray.ToString(), | |
company = developer["company"].AsString | |
}).ToList(); | |
return View(model); | |
} | |
static MongoDatabase retreive_mongohq_db() | |
{ | |
return MongoServer.Create( | |
ConfigurationManager.ConnectionStrings["MongoHQ"].ConnectionString) | |
.GetDatabase("t2"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment