Skip to content

Instantly share code, notes, and snippets.

@jasonmccay
Created October 9, 2011 05:17
Show Gist options
  • Save jasonmccay/1273340 to your computer and use it in GitHub Desktop.
Save jasonmccay/1273340 to your computer and use it in GitHub Desktop.
Create your Developer Controller
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