Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 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 IMongoCollection<Person> collection2 = db.GetCollection<Person>("comapnies");
  30.         private static int currentPage = 1, pageSize = 2, count = 1;
  31.         //private readonly MongoGridFS gf = new MongoGridFS(db);
  32.         static void Main(string[] args)
  33.         {
  34.  
  35.             Console.WriteLine("\n\n\n");
  36.  
  37.             //FindPeopleWithBlueEyes();    
  38.             //FindElders();
  39.             //AgeSort();
  40.             //ShowNames();
  41.  
  42.  
  43.             Console.ReadLine();
  44.  
  45.         }
  46.        
  47.  
  48.         public static void FindPeopleWithBlueEyes()
  49.         {
  50.             var personEyeColor = "blue";
  51.             var people = collection
  52.                 .Find(p => p.eyeColor == personEyeColor)
  53.                 .SortBy(p => p.index)
  54.                 .Limit(5)
  55.                 .ToListAsync()
  56.                 .Result;
  57.  
  58.             Console.WriteLine("Result:");
  59.             foreach (var person in people)
  60.             {
  61.                 Console.WriteLine(" * " + person.index);
  62.             }
  63.         }
  64.  
  65.         //SKIP
  66.         public static void FindElders()
  67.         {
  68.             var people = collection
  69.                 .Find(p => p.age > 40 && p.age < 70)
  70.                 .SortBy(p => p.index)
  71.                 .Limit(5)
  72.                 .ToListAsync()
  73.                 .Result;
  74.  
  75.             Console.WriteLine("Before skip:");
  76.             foreach (var person in people)
  77.             {
  78.                 Console.WriteLine($"{count}, \t {person.index} ");
  79.             }
  80.  
  81.             people = collection
  82.            .Find(p => p.age > 40 && p.age < 70)
  83.            .SortBy(p => p.index)
  84.            .Skip(2)
  85.            .Limit(5)
  86.            .ToListAsync()
  87.            .Result;
  88.  
  89.             Console.WriteLine("After skip(2):");
  90.             foreach (var person in people)
  91.             {
  92.                 Console.WriteLine($"{count}, \t {person.index} ");
  93.             }
  94.         }
  95.  
  96.         //SORT
  97.         public static async void AgeSort()
  98.         {
  99.             await collection.Find(FilterDefinition<Person>.Empty)
  100.                 .Limit(6)
  101.                 .Sort("{age: 1}")
  102.                 .ForEachAsync(
  103.                     p =>
  104.                     {
  105.                         Console.WriteLine($"{count},\t Index: {p.index}, Age: {p.age}, Name: {p.name.First().firstname}");
  106.                     });
  107.  
  108.         }
  109.  
  110.         //PROJECT
  111.         public static async void ShowNames()
  112.         {
  113.             await collection.Find(FilterDefinition<Person>.Empty)
  114.                 .Project(p => new { p.index, p.name })
  115.                 .ForEachAsync(
  116.                     p =>
  117.                     {
  118.                         Console.WriteLine($"{count},\t Index: {p.index}, Name: {p.name.First().firstname}, Surname: {p.name.Last().surname}");
  119.                         count++;
  120.                     });
  121.  
  122.  
  123.  
  124.         }
  125.  
  126.  
  127.         public static void sth()
  128.         {
  129.  
  130.         }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.     }
  137. }
  138.  
  139.  
  140. /*
  141. public class People
  142. {
  143.     public List<Person> people { get; set; }
  144. }*/
  145.  
  146. public class Person
  147. {
  148.     public ObjectId _id { get; set; }
  149.     public int index { get; set; }
  150.     public int age { get; set; }
  151.     public string eyeColor { get; set; }
  152.     public List<Name> name { get; set; }
  153.     public string gender { get; set; }
  154.     public string company { get; set; }
  155.     public string email { get; set; }
  156.     public string phone { get; set; }
  157.     public List<Address> address { get; set; }
  158. }
  159.  
  160. public class Name
  161. {
  162.     public string firstname { get; set; }
  163.     public string surname { get; set; }
  164. }
  165.  
  166. public class Address
  167. {
  168.     public int nr { get; set; }
  169.     public string street { get; set; }
  170.     public string city { get; set; }
  171. }
  172. /*
  173. public class Companies
  174. {
  175.     public List<Company> companiesList { get; set; }
  176. }*/
  177.  
  178. public class Company
  179. {
  180.     public ObjectId _id { get; set; }
  181.     public int index { get; set; }
  182.     public string name { get; set; }
  183.     public string city { get; set; }
  184.     public string averagesalary { get; set; }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement