released November 3, 2010 • v0.9 released December 6, 2010 • v0.11 released January 25, 2011 • v1.0 released March 23, 2011 • v1.1 released June 16, 2011 • v1.2 planned for late July, 2011
elements • A BsonElement has: • a name • a value (BsonValue) • BsonValue has a subtype for each BsonType • BsonInt32/BsonInt64 • BsonString • BsonDateTime • BsonArray • BsonDocument (embedded document) • and more…
BsonDocument( new BsonElement(“Author”, “Ernest Hemingway”), new BsonElement(“Title”, “For Whom the Bell Tolls”) ); // using collection initializer syntax var book = new BsonDocument { { “Author”, “Ernest Hemingway” }, { “Title”, “For Whom the Bell Tolls” } };
• MongoDatabase • MongoCollection<TDefaultDocument> • MongoCursor<TDocument> These top level classes are all thread safe. (MongoCursor is thread safe once frozen).
“mongodb://localhost:27017”; MongoServer server = MongoServer.Create(url); One instance is created for each URL. Subsequent calls to Create with the same URL return the same instance. Not [Serializable], so don’t put in session state.
each server it is connected to • The connections are shared among all threads • A connection is normally returned to the connection pool as soon as possible
new MongoCredentials(username, password); MongoDatabase test = server["test", credentials]; // or MongoDatabase test = server["test", SafeMode.True]; One instance is created for each combination of name, credentials and SafeMode. Subsequent calls with the same values return the same instance.
test.GetCollection("books"); // or specifying a default document type MongoCollection<Book> books = test.GetCollection<Book>("books"); Book is default document type
database.GetCollection<Book>( “books”, SafeMode.True); One instance is created for each combination of name, TDefaultDocument and SafeMode. Subsequent calls with the same values return the same instance.
new BsonDocument { { “Author”, “Ernest Hemingway” }, { “Title”, “For Whom the Bell Tolls” } }; SafeModeResult result = books.Insert(book); Returns a SafeModeResult (null if not using SafeMode). Consider using InsertBatch if inserting multiple documents.
and Title properties var books = database.GetCollection<Book>(“books”); var book = new Book { Author = “Ernest Hemingway”, Title = “For Whom the Bell Tolls” }; var result = books.Insert(book); The book instance will be serialized to a BSON document (see Serialization).
= books.FindOne(); // using POCOs var books = database[“books”]; var book = books.FindOneAs<Book>(); // returns Book instead of BsonDocument // or var books = database.GetCollection<Book>(“books”); var book = books.FindOne(); // returns Book because Book is default document type
var cursor = books.Find(query); foreach (var book in cursor) { // process matching book } // a more complicated query var query = Query.And( Query.EQ(“Author”, “Ernest Hemingway”), Query.GTE(“PublicationYear”, 1950) );
set on a cursor before it is enumerated (and before it is frozen) var query = Query.GTE(“PublicationYear”, 1950); var cursor = books.Find(query); cursor.Skip = 100; // probably want a sort order cursor.Limit = 10; foreach (var book in cursor) { // process 10 books (first 100 skipped) }
cursor = books.Find(query).SetSkip(100).SetLimit(10); foreach (var book in cursor) { // process 10 books (first 100 skipped) } // or more compactly var query = Query.GTE(“PublicationYear”, 1950); foreach (var book in books.Find(query).SetSkip(100).SetLimit(10)) { // process 10 books (first 100 skipped) }
calls Update var query = Query.EQ(“Title”, “For Who”); var book = books.FindOne(query); book.Title = “For Whom the Bell Tolls”; SafeModeResult result = books.Save(book); Save uses the _id value to decide whether to call Insert or Update.
occur on the same connection wrap the operations with calls to RequestStart/RequestDone • RequestStart is per thread • For the duration of the Request the thread holds and uses a single connection, which is not returned to the pool until RequestDone is called • Calls to RequestStart can be nested. The request ends when the nesting level reaches zero again.
value of RequestStart is a helper object that implements IDisposable • RequestDone is called for you automatically when the using statement terminates using (server.RequestStart(database)) { // a series of related operations }
you can always run them yourself var command = new CommandDocument(“collstats”, “books”); CommandResult result = database.RunCommand(command); CommandResult result = server.RunAdminCommand(“buildinfo”);
at • MongoServer (in the connection string) • MongoDatabase • MongoCollection • Individual operation • SafeMode options are set at the time the instance is created (required for thread safety)
documents • AutoMap: • Public fields and properties • Lots of ways to customize serialization • IBsonSerializable • IBsonSerializer (registered by calling BsonSerializer.RegisterSerializer) • Replace one or more default conventions • [BsonXyz] Attributes (like DataContract) • BsonClassMap.RegisterClassMap
files var gridFS = database.GridFS; var fileInfo = gridFS.Upload(“filename”); gridFS.Download(“filename”); // using Stream instead of filename var fileInfo = gridFS.Upload(stream, “remotename”); gridFS.Download(stream, “remotename”);
GridFS as if they were local files var gridFS = database.GridFS; var stream = gridFS.Create(“remotename”); // stream implements .NET’s Stream API // more examples var stream = gridFS.OpenRead(“remotename”); var stream = gridFS.Open(“remotename”, FileMode.Append);
fixes and low level (internal) changes • New Shell output mode is default for ToJson • New Powershell friendly methods (added non-generic versions of all generic methods) • Support for ShouldSerializeXyz methods • New MongoServer properties for replica sets • New MongoServerInstance class
Arbiters • Instances (or Instance if not using a replica set) • Passives • Primary • Secondaries • These properties are instances of the new MongoServerInstance class. Not valid until you Connect.
a logical server (which might be a replica set), MongoServerInstance represents an actual physical server • Interesting properties: • Address, BuildInfo, ConnectionPool, EndPoint, IsArbiter, IsPrimary, IsSecondary, MaxDocumentSize, MaxMessageLength
to an interface • Check element names (no “.” or “$”) • JsonReader improvements • JsonReader/Writer UUID helpers • Support new MongoDB 2.0 features • Release and Debug builds
and meetups http://www.10gen.com/events http://bit.ly/mongoX Facebook | Twitter | LinkedIn http://linkd.in/joinmongo download at mongodb.org support, training, and this talk brought to you by