MongoDB - Pobieranie danych

0

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:
screenshot-20190606115108.png

Jenak idąc dalej pojawia rzucany jest wyjątek:
screenshot-20190606115149.png

Wg. podpowiedzi Count() zwraca long
screenshot-20190606115340.png

ale wtedy też jest błąd:
screenshot-20190606115400.png

Jak to rozwiązać?

3

Czemu nie korzystasz z MongoDrivera ? Ten tutorial jest z 2010 roku. Wykorzystujesz w nim bibliotekę, która nie jest rozwijana od 9 lat.
https://docs.mongodb.com/ecosystem/drivers/csharp/

0

Wziąłem po prostu jeden z pierwszych wyników w wyszukiwarce :).

Zgodnie z radą użyłem MongoDB C#/.NET Driver, poszło dość gładko - mam dane których potrzebowałem :).

Dziękuję za pomoc.

1 użytkowników online, w tym zalogowanych: 0, gości: 1