2. Let me introduce myself… Stefano Paluello All around geek @ Barcrest Group & Pastesoft stefano.paluello@gmail.com stefanopaluello.wordpress.com Twitter: @palutz http://www.pastesoft.com
3. What is MongoDB? It’s an open source document oriented database: Full index support Replication & High Availability Scalable (auto-sharding) High performance (written in C++, atomic update,…) Rich query support Map/Reduce GridFS (store files without problem) Commercial support
4. Setting up MongoDB on Windows Download your version (better the 64bit Production release) from the official website: http://www.mongodb.org/downloads
5. Running MongoDB server… Best place where to look at (Windows quickstart): http://www.mongodb.org/display/DOCS/Quickstart+Windows Unzip, create log and data dir, run it
6. Ok. Are we running? Let’s connect to the shell and check it $mongodirinongo.exe
7. MongoDB can run as service Official guide: http://www.mongodb.org/display/DOCS/Windows+Service Easy way: $mongodirinongod –install Let’s add some “flavour”: $mongodirinongod --logpath .ogogs --logappend --dbpath .ata –directoryperdb –install To manage it, NET START mongos, NET STOP mongos
8. MongoDB C#/.Net driver Official, high performance, fully featured C# driver supported by 10gen Now at version 1.2 Built and tested against MongoDB 1.8.3 e 2.0.0 on : Visual Studio 2008, Visual Studio 2010 MonoDevelop 2.6 (Mono 2.10) It can be downloaded from github.com: http://github.com/mongodb/mongo-csharp-driver NuGet: PM> Install-Package mongocsharpdriver http://www.nuget.org/List/Packages/mongocsharpdriver
10. MongoDB C#/.Net driver Two main (separated) assemblies: MongoDB.Bson.dll MongoDB.Driver.dll (you can use other drivers) Namespaces (required) MongoDB.Bson; MongoDB.Driver; Namespace (additional) MongoDB.Bson.IO, MongoDB.Bson.Serialization; MongoDB.Bson.Serialization.*, MongoDB.Driver.GridFs
11. BSON Namespace Represent the BSON Object Model A set of classes that handle all the specification of BSON documents (I/O, serialization, in-memory object model) Main classes: BsonDocument BsonElement BsonValue (abstract class), with BsonTypeenum property BsonArray BsonDateTime BsonDocument BsonInt32 BsonObjectId BsonString
12. BSON Object Model BsonDocument is the main class BsonDocument is a collection of elements (BsonElements ) BsonElement is a name/value pair where the value is a BsonValue (with its own BsonType) The BsonElement class is seldom used directly cause it’s created as needed implicitly: document.Add(new BsonElement(“Name”, “Stefano”)); document.Add(“Name”, “Stefano”));
13. MongoDB.Driver Main classes (all thread-safe): MongoServer MongoDatabase MongoCollection<TDocument> MongoCursor<Tdocument> (only when frozen) MongoServersrv = MongoServer.Create(); Every URL has a MongoServer instance (different Create calls with the same URL return with the same MongoServer instance NOT put in the session state (NOT serializable)
14. MongoDB.Driver MongoServersrv = MongoServer.Create(); Every URL has a MongoServer instance different Create() calls with the same URL return with the same MongoServerinstance Do NOT put in the session state (NOT serializable) The driver manage a Connection Pools (one Connection Pools for every server) The connections are shared with all threads
15. MongoDB Driver MongoCollection : The class manages a collection of BsonDocument in a MongoDB database MongoCollection<TDefaultDocument>: Manage a collection of document, with the default type specified by the TDefaultDocument parameter GetCollection
16. MongoCollection class CRUD with Mongo – Query a collection FindOne and FindOneAs<T> IMongoQuery objects (QueryComplete) Find(query)
17. MongoCollection class CRUD with MongoDB - Insert There are many ways to insert data into a MongoDB collection (using the BsonDocument or any object that can be serialized