Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MongoDB.Driver;
  7. using MongoDB.Bson;
  8. using MongoDB.Driver.Core.Events;
  9. using MongoDB.Driver.GridFS;
  10.  
  11.  
  12. namespace nierelacyjneZadanie2
  13. {
  14.     class Program
  15.     {
  16.         /* public static MongoClient client = new MongoClient(new MongoClientSettings
  17.          {
  18.              Server = new MongoServerAddress("localhost"),
  19.              ClusterConfigurator = cb =>
  20.              {
  21.                  cb.Subscribe<CommandStartedEvent>(e =>
  22.                      Console.WriteLine($"{e.CommandName} - {e.Command.ToJson()}"));
  23.              }
  24.  
  25.          });*/
  26.         private static MongoClient client = new MongoClient();
  27.         private static IMongoDatabase db = client.GetDatabase("test");
  28.         private static IMongoCollection<Person> collection = db.GetCollection<Person>("people");
  29.         private static int currentPage = 1, pageSize = 2, count = 1;
  30.         //private readonly MongoGridFS gf = new MongoGridFS(db);
  31.         static void Main(string[] args)
  32.         {
  33.  
  34.             Console.WriteLine("\n\n\n");
  35.            
  36.             //FindPeopleWithBlueEyes();    
  37.             //FindElders();
  38.             //AgeSort();
  39.             //ShowNames();
  40.  
  41.  
  42.             Console.ReadLine();
  43.  
  44.         }
  45.  
  46.         public static void FindPeopleWithBlueEyes()
  47.         {
  48.             var personEyeColor = "blue";
  49.             var people = collection
  50.                 .Find(p => p.eyeColor == personEyeColor)
  51.                 .SortBy(p => p.index)
  52.                 .Limit(5)
  53.                 .ToListAsync()
  54.                 .Result;
  55.  
  56.             Console.WriteLine("Result:");
  57.             foreach (var person in people)
  58.             {
  59.                 Console.WriteLine(" * " + person.index);
  60.             }
  61.         }
  62.  
  63.         //SKIP
  64.         public static void FindElders()
  65.         {
  66.             var people = collection
  67.                 .Find(p => p.age > 40 && p.age < 70)
  68.                 .SortBy(p => p.index)
  69.                 .Limit(5)
  70.                 .ToListAsync()
  71.                 .Result;
  72.  
  73.             Console.WriteLine("Before skip:");
  74.             foreach (var person in people)
  75.             {
  76.                 Console.WriteLine($"{count}, \t {person.index} ");
  77.             }
  78.  
  79.                  people = collection
  80.                 .Find(p => p.age > 40 && p.age < 70)
  81.                 .SortBy(p => p.index)
  82.                 .Skip(2)
  83.                 .Limit(5)
  84.                 .ToListAsync()
  85.                 .Result;
  86.  
  87.             Console.WriteLine("After skip(2):");
  88.             foreach (var person in people)
  89.             {
  90.                 Console.WriteLine($"{count}, \t {person.index} ");
  91.             }
  92.         }
  93.  
  94.         //SORT
  95.         public static async void AgeSort()
  96.         {
  97.             await collection.Find(FilterDefinition<Person>.Empty)
  98.                 .Limit(6)
  99.                 .Sort("{age: 1}")
  100.                 .ForEachAsync(
  101.                     p =>
  102.                     {
  103.                         Console.WriteLine($"{count},\t Index: {p.index}, Age: {p.age}, Name: {p.name.First().firstname}");
  104.                     });
  105.            
  106.         }
  107.  
  108.         //PROJECT
  109.         public static async void ShowNames()
  110.         {
  111.             await collection.Find(FilterDefinition<Person>.Empty)
  112.                 .Project(p => new {p.index, p.name})
  113.                 .ForEachAsync(
  114.                     p =>
  115.                     {
  116.                         Console.WriteLine($"{count},\t Index: {p.index}, Name: {p.name.First().firstname}, Surname: {p.name.Last().surname}");
  117.                         count++;
  118.                     });
  119.  
  120.  
  121.  
  122.         }
  123.  
  124.  
  125.         public static void sth()
  126.         {
  127.  
  128.         }
  129.  
  130.  
  131.  
  132.    
  133.  
  134.     }
  135. }
  136.  
  137.  
  138. /*
  139. public class People
  140. {
  141.     public List<Person> people { get; set; }
  142. }*/
  143.  
  144. public class Person
  145. {
  146.     public ObjectId _id { get; set; }
  147.     public int index { get; set; }
  148.     public int age { get; set; }
  149.     public string eyeColor { get; set; }
  150.     public List<Name> name { get; set; }
  151.     public string gender { get; set; }
  152.     public string company { get; set; }
  153.     public string email { get; set; }
  154.     public string phone { get; set; }
  155.     public List<Address> address { get; set; }
  156. }
  157.  
  158. public class Name
  159. {
  160.     public string firstname { get; set; }
  161.     public string surname { get; set; }
  162. }
  163.  
  164. public class Address
  165. {
  166.     public int nr { get; set; }
  167.     public string street { get; set; }
  168.     public string city { get; set; }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement