Advertisement
NgThanhPhuc

MongoDB_FindFilterProjections

Feb 12th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using MongoDB.Bson;
  2. using MongoDB.Bson.IO;
  3. using MongoDB.Bson.Serialization;
  4. using MongoDB.Driver;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace M101DotNet
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             MainAsync(args).GetAwaiter().GetResult();
  17.             Console.WriteLine("Press enter");
  18.             Console.ReadLine();
  19.  
  20.         }
  21.  
  22.         static async Task MainAsync(string[] args)
  23.         {
  24.             var client = new MongoClient();
  25.             var db = client.GetDatabase("Phucdb");
  26.             var col = db.GetCollection<Person>("users");
  27.                        
  28.             var list = await col.Find("{}")
  29.                 .Project(x => new {x.Name, x.Profesion})
  30.                 .ToListAsync();
  31.             foreach (var doc in list)
  32.             {
  33.                 Console.WriteLine(doc);
  34.             }        
  35.         }
  36.         private class Person
  37.         {
  38.             public ObjectId Id { get; set; }
  39.             public string Name { get; set; }
  40.             public int Age { get; set; }
  41.             public string Profesion { get; set; }
  42.             public override string ToString()
  43.             {
  44.                 return string.Format("Id: {0}, Name: \"{1}\", Age: {2}, Profession: \"{3}\"", Id, Name, Age, Profesion);
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement