Skip to content

Instantly share code, notes, and snippets.

@atheken
Created May 24, 2010 23:38
Show Gist options
  • Save atheken/412568 to your computer and use it in GitHub Desktop.
Save atheken/412568 to your computer and use it in GitHub Desktop.
public class Widget
{
public ObjectId Id {get;set;}
public String Color {get;set;}
public double Price {get;set;}
public DateTime ReleaseDate {get;set;}
public IEnumerable Reviews {get;set;}
}
//Next, spool up a connection to your database
//(The DB doesn't have to exist yet, but MongoDB DOES need to be running)
using(var db = Mongo.Create("mongo://localhost/ProductDB")
{
//Get a reference to the collection in which we want to
//store our Widgets (doesn't have to exist yet.)
var widgets = db.GetCollection();
//create a widget instance.
var topSellingWidget = new Widget{ Id = ObjectId.NewObjectId(), Color = "Red", Price = 39.95,
ReleaseDate = DateTime.Now, Reviews = Enumerable.Empty() };
//now, save the instance
widgets.Save(topSellingWidget);
//lastly, retrieve it from the DB.
var hydratedTopSellingWidgetFromDB = widgets.FindOne(new {Color = "Red",Price = 39.95});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment