MongoDB quickstart tutorial in C# and Linqpad
Here is my quick start tutorial:
If you dont already have a MongoDb installation I recommend going for the hosted option here: https://mongohq.com/home (free option available)
You then need to download the official 10gen C# driver: https://github.com/mongodb/mongo-csharp-driver/downloads
Finally you should download Linqpad, I really love this app, you can use it as a c# scratchpad to rapidly mess about with any new libraries or code bases and is a great alternative to running a console app. Just edit your code and hit F5 to run the app. Its especially useful when you want to dump out objects structures to the screen visually, in this case MongoDB data. You can download LinqPad here: http://www.linqpad.net/
Super Quickstart:
- https://mongohq.com/home – Create a database here
- https://github.com/mongodb/mongo-csharp-driver/downloads – Download the Driver
- http://www.linqpad.net/ – Download this badboy
Configuring Linqpad
There is a quick configuration element to Linqpad, we have to add the driver Dll reference and namespace:
First add the Dlls
Now add the namespaces:
Mongolific! Now we are ready to rock and roll!
Here is a simple example to insert a document (an object class) into a table, and then retreive it again using a simple query.
Here is the code for copy and paste land
void Main() { var server = MongoServer.Create("mongodb://law:pass@pearl.mongohq.com:27071/"); var db = server.GetDatabase("Universe"); var pluto = new Planet() { Name = "Pluto", Colour = "Purple"; }; collection.Insert(pluto); var query = Query.And(Query.EQ("Name", "Pluto")); var list = collection.Find(query).ToList(); list.Dump(); } public class Planet { public ObjectId Id { get; set; } public string Name { get; set; } public string Colour { get; set; } }
Simples!
.Net silicon valley发表评论
SRBdlM Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to
rtYuMK You could certainly see your expertise in the work you write.
vRCuZe Its hard to find good help I am constantnly proclaiming that its hard to find quality help, but here is
bSLmEx This is a topic that as near to my heart Thank you! Where are your contact details though?
NR0VDV It is truly a nice and helpful piece of information. I am glad that you just shared this helpful info with us. Please keep us informed like this. Thanks for sharing.
AZOEJl Very informative post. Great.
dqEkup I cannot thank you enough for the blog article.
9ezpp3 Thanks-a-mundo for the blog.Really looking forward to read more. Fantastic.
Lg6Iw9 Im obliged for the article.Really looking forward to read more. Will read on...
iconX0 I truly appreciate this post.Thanks Again. Cool.
oaJ0yq I cannot thank you enough for the blog.Much thanks again. Keep writing.
collection 出错呀?什么原因