C# Development (Sam Corder)

14
and .Net Getting Started in 30 Minutes or Less Sam Corder @SamCorder [email protected]

Transcript of C# Development (Sam Corder)

Page 1: C# Development (Sam Corder)

and .NetGetting Started in 30 Minutes or Less

Sam Corder@SamCorder

[email protected]

Page 2: C# Development (Sam Corder)

Baseline

• MongoDB?

• .Net?

Page 3: C# Development (Sam Corder)

Statically Dynamic• Challenges

• Model a dynamic database in a static language

• Javascript(json/bson) is not 1:1 with .Net

• Compromises

• Documents

• Certain conventions (Arrays & Dates)

Page 4: C# Development (Sam Corder)

API Overview

• Mongo - Entry point into server

• Database - Operations on a Database

• Collection - Queries

• Document - Basic unit

• Cursor - Holds query results

* Database and Collection to be prefixed with “Mongo” in v 0.90

Page 5: C# Development (Sam Corder)

Mongo Types

.Net TypeOid

CodeCodeWScope

Binary

DBRef

MongoRegex

Mongo Type

ObjectID _id

Javascript

Binary

DBRef

Javascript regex

Page 6: C# Development (Sam Corder)

Diving In

Page 7: C# Development (Sam Corder)

Getting Connected<?xml version="1.0" encoding="utf-8"?><configuration>  <configSections>    <section name="Mongo" type="MongoDB.Driver.Configuration.MongoConfigurationSection, MongoDB.Driver" />  </configSections>  <Mongo>    <connections>      <add key="tests" connectionString="Server=localhost:27017" />    </connections>  </Mongo></configuration>

string connstr = ConfigurationManager.AppSettings["simple"];if(String.IsNullOrEmpty(connstr)) throw new ArgumentNullException("Connection string not found.");            mongo = new Mongo(connstr);mongo.Connect();

ShowUsing();var mongo = new Mongo();mongo.Connect();

Page 8: C# Development (Sam Corder)

Insertslibrary = mongo["library"];albums = library["albums"];albums.Insert(new Document(){{"Artist", "Seth Walker"}, {"Title", "Leap of Faith"},

{"Genre", "Blues"}});albums.Insert(new Document(){{"Artist", "BB King"}, {"Title", "Lucille"},

{"Genre", "Blues"}}, true);

• Insert a single document at a time

• Insert multiples with IEnumerable

• Safemode for when you need to know it got there

• _id Automatically added to documents

var albums = db["albums"];Describe(albums);

albums.Insert(new Document(){{"Title", "Lucille"}, {"Artist", "BB King"}});

Show in shell

Page 9: C# Development (Sam Corder)

Finding Our Data

• Results lazily loaded in batches

• Cursor should be disposed

• Javascript using CodeWScope and $where

var cursor = albums.Find(new Document(){{"Artist", "BB King"}});cursor.Sort("Artist").Limit(5); //Fluent addition of criteriausing(cursor){    foreach(Document doc in cursor.Documents){        Console.WriteLine(doc.ToString());    }}

var doc = albums.FindOne(new Document());doc.ToString();

Page 10: C# Development (Sam Corder)

Updating

• By default replaces document

• $ ($set) modifiers for partial update

• Use Save() for individual documents

• UpdateAll with a modifier query and a spec

Document lucille = albums.FindOne(new Document(){{"Artist", "BB King"}, {"Title", "Lucille"}});var id = lucille["_id"]; //Oidlucille["Released"] = 1968;lucille["Songs"] = new Document[]{    new Document(){{"Title","Lucille"},{"length", "10:16"}},    new Document(){{"Title","You Move Me So"},{"length", "2:03"}},    new Document(){{"Title","Stop Puttin the Hurt on Me"},{"length", "3:04"}},    new Document(){{"Title","Watch Yourself"},{"length", "5:47"}}};albums.Save(lucille);albums.Update(lucille, new Document(){{"_id", id}}); //Same as above

doc["Genre"] = "Blues";albums.Save(doc);var doc = albums.FindOne(new Document());doc.ToString();

Page 11: C# Development (Sam Corder)

Deleting

• Delete takes a query like Find

• Careful and empty Document deletes all

//remove documents with the genre Rock.albums.Delete(new Document(){{"Genre", "Rock"}});//remove everything from the albums collection.albums.Delete(new Document());

Page 12: C# Development (Sam Corder)

Additional Features• Connection Pooling

• Various extra functions (Count, Hint, Explain, FindAndModify, etc.)

• GridFS via Streams

• Map Reduce

• Execute Javascript on server

• Mono

Page 13: C# Development (Sam Corder)

Heading to 1.0

• Type safe Collections IMongoCollection<T>

• Improved Linq support

• Projections, Aggregates, Conditions, Skip, Take

• Updated GridFS API