Skip to content
joelvh edited this page Oct 29, 2010 · 3 revisions

An example of using NoRM. This has been developed and tested using Mono 2.6.4 on Linux.

Model:

 public class Product
  {
    public Product()
    {
      // Default values are to be set here
      Shipping = new Address();
    }
    public ObjectId _id{get;set;}
    public double Price{get;set;}
    public Address Shipping{get;set;}
    public DbReference<Brand> TheBrand{get;set;}
    public string Name{get;set;}
    public override string ToString ()
    {
      return string.Format ("[Product: Price={0}, Shipping={1}, Name={2}]", Price, Shipping, Name);
    }
  }
  public class Brand
  {
    public Brand()
    {
      Suppliers = new List<Address>();
    }
    public ObjectId _id{get;set;}
    public string Name{get;set;}
    public int CustomerRating{get;set;}
    public List<Address> Suppliers{get;set;}
    public override string ToString ()
    {
      return string.Format ("[Brand: Name={0}, CustomerRating={1}", Name, CustomerRating);
    }
  }
  public class Address
  {
    public Address()
    {
    }
    public ObjectId _id{get;set;}
    public string StreetName{get;set;}
    public override string ToString ()
    {
      return string.Format ("[Address: _id={0}]", _id);
    }
  }

Opening a database connection:

 using(var db = Mongo.Create("mongodb://localhost/test"))
  {
      var prodCollection = db.GetCollection<Product>();
      var brandCollection = db.GetCollection<Brand>();
      // your code here.
  }

Constructing complex documents and saving.

 // create four addresses
  List<Address> places = new List<Address>();
  for(int i = 0; i < 6; i++)
      places.Add(new Address{StreetName=String.Format("#{0}, Street St", i)});
  // create brands
  Brand acme = new Brand{Name="Acme Inc.", CustomerRating = 8};
  acme.Suppliers.Add(places[0]);
  acme.Suppliers.Add(places[1]);
  brandCollection.Save(acme);
  //
  Brand apple = new Brand{Name = "Apple, Inc.", CustomerRating = 7};
  apple.Suppliers.Add( new Address{StreetName = "Infinite Loop"});
  brandCollection.Save(apple);

Creating database references:

 // Create products
  Product rocketBoots = new Product{Price = 100, Shipping = places[4], Name="Rocket Boots"};
  rocketBoots.TheBrand = new DbReference<Brand>(acme._id);
  prodCollection.Save(rocketBoots);
  // 
  Product explosives = new Product{Price = 10, Shipping = places[2], Name = "Explosives"};
  explosives.TheBrand = new DbReference<Brand>(acme._id);
  //
  Product iPod = new Product{Price=350, Shipping = places[5], Name = "iPod Mini"};
  iPod.TheBrand = new DbReference<Brand>(apple._id);
  prodCollection.Save(iPod);

Reading data and DB References:

 using(var db = Mongo.Create("mongodb://localhost/test"))
  {
      var prod = db.GetCollection<Product>();
      var brand = db.GetCollection<Brand>();      
      // iterate over the products
      foreach(var product in prod.AsQueryable().ToList())
      {
          // Retrieve a brand from a database reference
          Brand theBrand = product.TheBrand.Fetch(() => db);
          //
          Console.WriteLine ("Available: {0} {1}", product.ToString(), theBrand.ToString());
      }
  }

Advanced Query examples

This is an article by Tadeusz Wojcik with many examples and operators, comparing the native MongoDB queries to NoRM using anonymous objects and LINQ: NoSql No Problem - NoRM and MongoDB Tutorial - Querying

Notes
TODO