Click here to Skip to main content
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads
 
Add your own
alternative version

Using MongoDB with the Official C# Driver

, 24 Oct 2011 CPOL
Introduces how to use MongoDB in your ASP.NET MVC project using the official C# driver.
IntroMongo_src.zip
IntroMongo
IntroMongo
bin
EntityFramework.dll
IntroMongo.dll
MongoDB.Bson.dll
MongoDB.Driver.dll
Content
themes
base
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Controllers
Global.asax
IntroMongo.csproj.user
Models
obj
Debug
TempPE
Properties
Scripts
Views
Remark
Shared
User
packages
EntityFramework.4.1.10331.0
EntityFramework.4.1.10331.0.nupkg
lib
EntityFramework.dll
jQuery.1.5.1
Content
Scripts
jQuery.1.5.1.nupkg
jQuery.UI.Combined.1.8.11
Content
Content
themes
base
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Scripts
jQuery.UI.Combined.1.8.11.nupkg
jQuery.Validation.1.8.0
Content
Scripts
jQuery.Validation.1.8.0.nupkg
jQuery.vsdoc.1.5.1
Content
Scripts
jQuery.vsdoc.1.5.1.nupkg
Modernizr.1.7
Content
Scripts
Modernizr.1.7.nupkg
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MongoDB.Driver;
using MongoDB.Bson;
using IntroMongo.Models;
using MongoDB.Driver.Builders;

namespace IntroMongo.Controllers
{
    public class UserController : Controller
    {
        public ActionResult Index()
        {
            // Get all users by sorting by createdate
            var users = MongoWrapper.GetDatabase().
                            GetCollection("users").
                            FindAll().
                            SetSortOrder(SortBy.Descending("createdate"));

            return View(users);
        }

        public ActionResult Edit(string id)
        {
            // Obtain id compatible with MongoDB to query
            var objectId = ObjectId.Parse(id);

            // Get user by id
            var user = MongoWrapper.GetDatabase().
                            GetCollection("users").
                            FindOneByIdAs<User>(ObjectId.Parse(id));

            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(string id, User userToEdit)
        {
            // Get user collection reference
            var users = MongoWrapper.GetDatabase().GetCollection("users");

            // Update user by id
            var user = users.FindOneById(ObjectId.Parse(id));

            user["firstname"] = userToEdit.FirstName;
            user["lastname"] = userToEdit.LastName;
            user["age"] = userToEdit.Age;

            // Update user object
            users.Save(user);

            return RedirectToAction("Index");
        }

        public ActionResult New()
        {
            return View();
        }

        [HttpPost]
        public ActionResult New(string id, User userToEdit)
        {
            // Get user collection reference
            var users = MongoWrapper.GetDatabase().GetCollection("users");

            // Create BsonDocument object for new user
            var user = new BsonDocument();
            user["firstname"] = userToEdit.FirstName;
            user["lastname"] = userToEdit.LastName;
            user["age"] = userToEdit.Age;
            user["createdate"] = DateTime.Now;

            // Insert new user object to collection
            users.Insert(user);

            return RedirectToAction("Index");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ercan Anlama
Software Developer
Turkey Turkey
I started to development during university years and worked in many freelance projects. I am working in software development more than 4 years and currently, working as analyst software developer in an Holland centre company in Istanbul, Turkey. I am interested in innovation and creativity in software development and passionate in learning new stuff.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 24 Oct 2011
Article Copyright 2011 by Ercan Anlama
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid