Wednesday, December 3, 2014

Pluralsight Mongodb ASP.NET MVC get started after install MongoDB locally




code to access client


Add database name to settings


add database name to code

can use getcollection to get database


test if works


showing works


Isolate Mongodb connection code to a context class





put client access code in here:



Add to home controller, and use this to access the database






Use Resharper to do the tests

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; namespace Tests { using MongoDB.Bson; using NUnit.Framework; public class BsonDocumentTests { [Test] public void EmptyDocument() { var document = new BsonDocumentTests(); Console.WriteLine(document.ToJson()); } [Test] public void AddElements() { var person = new BsonDocument(); person.Add("firstname", new BsonString("bob")); Console.WriteLine(person); } [Test] public void BsonValueConversion() { var person = new BsonDocument { {"age", 54} }; Console.WriteLine((person["age"].ToDouble()+10)); Console.WriteLine((person["age"].IsInt32)); Console.WriteLine((person["age"].IsString)); } [Test] public void ToBson() { var person = new BsonDocument(); person.Add("firstname", new BsonString("bob")); var bson = person.ToBson(); Console.WriteLine(BitConverter.ToString(bson)); var deserializedPerson = BsonSerializer.Deserialize<BsonDocument>(bson); Console.WriteLine(deserializedPerson); } public class Person { public string Firstname { get; set; } public int Age { get; set; } } [Test] public void Automatic() { var person = new Person() { Age = 54, Firstname = "bob" }; Console.WriteLine(person.ToJson()); } } }

No comments:

Post a Comment