Thursday 8 November 2012

C# Mongo Driver Example

Read the official setup guide first.

http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial

This is a full example  for a console,  should not need much explaining really, just run and tinker

It saves a collection to MongoDB if not created and get the collection back again.

Try changing the object saved and comment out the create code after its first run to see that the items are still in the database.

Remember you can always stop the db and delete the database files (usually in /data/db/*.*) if you want.



using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;

namespace MongoTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var getPeople = DoStuff.GetPeople();
            if (getPeople.Count == 0)
            {
                DoStuff.PopulateDb();
                getPeople = DoStuff.GetPeople();
            }
            DoStuff.DisplayResults(getPeople);
            Console.ReadLine();
        }     
    }

    public static class DoStuff
    {
        public static void DisplayResults(IEnumerable people )
        {
            foreach (var p in people)
            {
                Console.WriteLine("id : " + p.Id + " Name : " + p.Name + " phone : " + p.Name);
            }
        }

        public static List GetPeople()
        {
            var result = new List();
            var connectionString = "mongodb://localhost/?safe=true";
            var server = MongoServer.Create(connectionString);
            var database = server.GetDatabase("test");
            var collection = database.GetCollection("people");
            var getall = collection.FindAll().ToList();
            result.AddRange(getall);           
            return result;
        }

        public static void PopulateDb()
        {
            var connectionString = "mongodb://localhost/?safe=true";
            var server = MongoServer.Create(connectionString);
            var database = server.GetDatabase("test");
            var collection = database.GetCollection("people");

            var p1 = new Person() {  Name = "Bob", Phone = "0341 555 2123"};
            var p2 = new Person() {  Name = "Ann", Phone = "0441 555 5656" };
            var p3 = new Person() {  Name = "Sue", Phone = "0541 555 1767" };
            var p4 = new Person() {  Name = "Sam", Phone = "0641 555 1234" };
            var p5 = new Person() {  Name = "Rob", Phone = "0641 555 3499" };
            var p6 = new Person() {  Name = "Ary", Phone = "0941 555 2323" };
            var p7 = new Person() {  Name = "Ian", Phone = "0641 555 0000" };

            collection.Insert(p1);
            collection.Insert(p2);
            collection.Insert(p3);
            collection.Insert(p4);
            collection.Insert(p5);
            collection.Insert(p6);
            collection.Insert(p7);

        }
    }

    public class Person
    {
        public ObjectId Id { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
    }

}



No comments:

Post a Comment

Comments are welcome, but are moderated and may take a wee while before shown.