Using MongoDB with the .Net Framework
-
Upload
stefano-paluello -
Category
Technology
-
view
5.913 -
download
3
description
Transcript of Using MongoDB with the .Net Framework
+
Using MongoDB on Windows with the .Net Framework and C#
Let me introduce myself…
Stefano Paluello All around geek @
Barcrest Group & Pastesoft
[email protected] stefanopaluello.wordpress.co
m Twitter: @palutz http://www.pastesoft.com
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
Setting up MongoDB on Windows
Download your version (better the 64bit Production release) from the official website: http://www.mongodb.org/downloads
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
Ok. Are we running?
Let’s connect to the shell and check it $mongodir\bin\mongo.exe
MongoDB can run as service
Official guide: http://
www.mongodb.org/display/DOCS/Windows+Service
Easy way: $mongodir\bin\mongod –install
Let’s add some “flavour”: $mongodir\bin\mongod --logpath .\log\logs --
logappend --dbpath .\data –directoryperdb –install To manage it, NET START mongos, NET STOP
mongos
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
MongoDB C#/.Net driver on NuGet
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
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 BsonType enum property
BsonArray BsonDateTime BsonDocument BsonInt32 BsonObjectId BsonString
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”));
MongoDB.Driver
Main classes (all thread-safe): MongoServer MongoDatabase MongoCollection<TDocument> MongoCursor<Tdocument> (only when
frozen) MongoServer srv = 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)
MongoDB.Driver
MongoServer srv = MongoServer.Create(); Every URL has a MongoServer instance different Create() calls with the same URL
return with the same MongoServer instance 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
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
MongoCollection class
CRUD with Mongo – Query a collection FindOne and FindOneAs<T> IMongoQuery objects (QueryComplete) Find(query)
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
MongoCollection class
CRUD with MongoDB – Update and Save There are many ways to update a document
MongoCollection class
CRUD with MongoDB – Delete There are many ways to delete a Document
Let’s play a bit with the code…
Useful (IMHO) tools
MongoVue (www.mongovue.com)
Chrome Webstore – Mongo Live
Another code demo
Get dynamics with C#