Witam.
Muszę jednorazowo wyciągnąć dane z bazy MongoDB.
Nigdy wcześniej nie miałem z tą bazą doczynienia, akcja jest jednorazowa, więc posiłkuje się tutorialem z sieci, niestety napotkałem problem...
Kod wygląda tak:
//Create a default mongo object. This handles our connections to the database.
//By default, this will connect to localhost, port 27017 which we already have running from earlier.
var mongo = new Mongo("mongodb://10.10.1.2/");
mongo.Connect();
//Get the blog database. If it doesn't exist, that's ok because MongoDB will create it
//for us when we first use it. Awesome!!!
var db = mongo.GetDatabase("blog");
//Get the Post collection. By default, we'll use the name of the class as the collection name. Again,
//if it doesn't exist, MongoDB will create it when we first use it.
var collection = db.GetCollection<Post>("Post");
//this deletes everything out of the collection so we can run this over and over again.
collection.Delete(p => true);
//Create posts to enter into the database.
CreatePosts(collection);
//count all the Posts
var totalNumberOfPosts = collection.Count();
//count only the Posts that have 1 comment
var numberOfPostsWith1Comment = collection.Count(p => p.Comments.Count == 2);
//find the titles of the posts that Jane commented on...
var postsThatJaneCommentedOn = from p in collection.Linq()
where p.Comments.Any(c => c.Email.StartsWith("Jane"))
select p.Title;
//find the titles and comments of the posts that have comments after January First.
var postsWithJanuary1st = from p in collection.Linq()
where p.Comments.Any(c => c.TimePosted > new DateTime(2010, 1, 1))
select new { Title = p.Title, Comments = p.Comments };
//find posts with less than 40 characters
var postsWithLessThan10Words = from p in collection.Linq()
where p.CharCount < 40
select p;
Zgodnie z opisami po wykonaniu:
var mongo = new Mongo("mongodb://10.10.1.2/");
mongo.Connect();
var db = mongo.GetDatabase("blog");
var collection = db.GetCollection<Post>("Post");
collection.Delete(p => true);
CreatePosts(collection);
stworzyła się baza danych, z kolekcją Post, wypełniona danymi:
Jenak idąc dalej pojawia rzucany jest wyjątek:
Wg. podpowiedzi Count()
zwraca long
ale wtedy też jest błąd:
Jak to rozwiązać?