Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Threading.Tasks;
- using MongoDB.Driver;
- using MongoDB.Bson;
- using MongoDB.Driver.Core.Events;
- using MongoDB.Driver.GridFS;
- namespace nierelacyjneZadanie2
- {
- class Program
- {
- /* public static MongoClient client = new MongoClient(new MongoClientSettings
- {
- Server = new MongoServerAddress("localhost"),
- ClusterConfigurator = cb =>
- {
- cb.Subscribe<CommandStartedEvent>(e =>
- Console.WriteLine($"{e.CommandName} - {e.Command.ToJson()}"));
- }
- });*/
- private static MongoClient client = new MongoClient();
- private static IMongoDatabase db = client.GetDatabase("test");
- private static IMongoCollection<Person> collection = db.GetCollection<Person>("people");
- private static IMongoCollection<Person> collection2 = db.GetCollection<Person>("comapnies");
- private static int currentPage = 1, pageSize = 2, count = 1;
- //private readonly MongoGridFS gf = new MongoGridFS(db);
- static void Main(string[] args)
- {
- Console.WriteLine("\n\n\n");
- //FindPeopleWithBlueEyes();
- //FindElders();
- //AgeSort();
- //ShowNames();
- Console.ReadLine();
- }
- public static void FindPeopleWithBlueEyes()
- {
- var personEyeColor = "blue";
- var people = collection
- .Find(p => p.eyeColor == personEyeColor)
- .SortBy(p => p.index)
- .Limit(5)
- .ToListAsync()
- .Result;
- Console.WriteLine("Result:");
- foreach (var person in people)
- {
- Console.WriteLine(" * " + person.index);
- }
- }
- //SKIP
- public static void FindElders()
- {
- var people = collection
- .Find(p => p.age > 40 && p.age < 70)
- .SortBy(p => p.index)
- .Limit(5)
- .ToListAsync()
- .Result;
- Console.WriteLine("Before skip:");
- foreach (var person in people)
- {
- Console.WriteLine($"{count}, \t {person.index} ");
- }
- people = collection
- .Find(p => p.age > 40 && p.age < 70)
- .SortBy(p => p.index)
- .Skip(2)
- .Limit(5)
- .ToListAsync()
- .Result;
- Console.WriteLine("After skip(2):");
- foreach (var person in people)
- {
- Console.WriteLine($"{count}, \t {person.index} ");
- }
- }
- //SORT
- public static async void AgeSort()
- {
- await collection.Find(FilterDefinition<Person>.Empty)
- .Limit(6)
- .Sort("{age: 1}")
- .ForEachAsync(
- p =>
- {
- Console.WriteLine($"{count},\t Index: {p.index}, Age: {p.age}, Name: {p.name.First().firstname}");
- });
- }
- //PROJECT
- public static async void ShowNames()
- {
- await collection.Find(FilterDefinition<Person>.Empty)
- .Project(p => new { p.index, p.name })
- .ForEachAsync(
- p =>
- {
- Console.WriteLine($"{count},\t Index: {p.index}, Name: {p.name.First().firstname}, Surname: {p.name.Last().surname}");
- count++;
- });
- }
- public static void sth()
- {
- }
- }
- }
- /*
- public class People
- {
- public List<Person> people { get; set; }
- }*/
- public class Person
- {
- public ObjectId _id { get; set; }
- public int index { get; set; }
- public int age { get; set; }
- public string eyeColor { get; set; }
- public List<Name> name { get; set; }
- public string gender { get; set; }
- public string company { get; set; }
- public string email { get; set; }
- public string phone { get; set; }
- public List<Address> address { get; set; }
- }
- public class Name
- {
- public string firstname { get; set; }
- public string surname { get; set; }
- }
- public class Address
- {
- public int nr { get; set; }
- public string street { get; set; }
- public string city { get; set; }
- }
- /*
- public class Companies
- {
- public List<Company> companiesList { get; set; }
- }*/
- public class Company
- {
- public ObjectId _id { get; set; }
- public int index { get; set; }
- public string name { get; set; }
- public string city { get; set; }
- public string averagesalary { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement