Advertisement
Kikku80

Untitled

Aug 9th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. // Requires official C# and .NET MongoDB Driver 2.5+
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using MongoDB.Bson;
  10. using MongoDB.Driver;
  11.  
  12. namespace ConsoleApp1
  13. {
  14.     class Program
  15.     {
  16.         static async Task _Main()
  17.         {
  18.  
  19.             IMongoClient client = new MongoClient("mongodb://localhost:27017/");
  20.             IMongoDatabase database = client.GetDatabase("EmsIssuing");
  21.             //IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("BalanceHistory");
  22.            
  23.             // Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/
  24.             var fields = new HashSet<string>();
  25.             fields.Add("_id");
  26.             fields.Add("When");
  27.             fields.Add("WalletId");
  28.             fields.Add("CardId");
  29.             fields.Add("MaskedPan");
  30.             fields.Add("CardholderName");
  31.             fields.Add("Description");
  32.             fields.Add("TransactionType");
  33.             fields.Add("LifeCycle");
  34.             fields.Add("Source");
  35.             fields.Add("TransactionRegion");
  36.             fields.Add("Currency");
  37.             fields.Add("Amount");
  38.             fields.Add("Fee");
  39.             fields.Add("AvailableBalance");
  40.             fields.Add("LedgerBalance");
  41.  
  42.             // Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/
  43.  
  44.             //var filter = RunFilter();
  45.             int fromDate = 1, toDate = 2;
  46.             GenerateCSV();
  47.  
  48.             void GenerateCSV()
  49.         {
  50.               var result = database.GetCollection<BsonDocument>("BalanceHistory").Find(RunFilter(31,1));
  51.                 var csv = new StringBuilder();
  52.                 string headerLine = string.Join(",", fields);
  53.            
  54.                 csv.AppendLine(headerLine);
  55.  
  56.                 foreach (var element in result.ToListAsync().Result)
  57.                 {
  58.                     string line = null;
  59.                     var counter = 0;
  60.                     foreach (var field in fields)
  61.                     {
  62.                         counter++;
  63.                         BsonValue value;
  64.  
  65.                         try
  66.                         {
  67.                             value = element.GetElement(field).Value;
  68.                         }
  69.                         catch (Exception e)
  70.                         {
  71.                             //todo add logging here
  72.                             line = line + ",";
  73.                             continue;
  74.                         }
  75.                    
  76.                         // Example deserialize to string
  77.                         switch (value.BsonType)
  78.                         {
  79.                             case BsonType.ObjectId:
  80.                             case BsonType.String:
  81.                             case BsonType.DateTime:
  82.                                 line += value;
  83.                                 break;
  84.                             case BsonType.Int32:
  85.                                 line += value.AsInt32;
  86.                                 break;
  87.                             case BsonType.Int64:
  88.                                 line += value.AsInt64;
  89.                                 break;
  90.                         }
  91.  
  92.                         if (counter != fields.Count)
  93.                         {
  94.                             line += ",";
  95.                         }
  96.                     }
  97.                     csv.AppendLine(line);
  98.                 }
  99.  
  100.                 var path = $"D:\\Temp\\Reports\\31";
  101.                 Directory.CreateDirectory(path);
  102.                 File.WriteAllText($"{path}\\EmsIssuing.BalanceHistory.csv", csv.ToString());
  103.         }
  104.            
  105.         }
  106.  
  107.         public static BsonDocument RunFilter(int fromDate, int ToDate)
  108.         {
  109.             BsonDocument filter = new BsonDocument();
  110.             filter.Add("$and", new BsonArray()
  111.                 .Add(new BsonDocument()
  112.                     .Add("When", new BsonDocument()
  113.                         .Add("$gte", new BsonDateTime(DateTime.ParseExact($"2019-07-{fromDate:D2} 01:00:00.000+0200", "yyyy-MM-dd HH:mm:ss.fffzzz", CultureInfo.InvariantCulture)))
  114.                     )
  115.                 )
  116.                 .Add(new BsonDocument()
  117.                     .Add("When", new BsonDocument()
  118.                         .Add("$lt", new BsonDateTime(DateTime.ParseExact($"2019-08-{ToDate:D2} 01:00:00.000+0200", "yyyy-MM-dd HH:mm:ss.fffzzz", CultureInfo.InvariantCulture)))
  119.                     )
  120.                 )
  121.             );
  122.             return filter;
  123.         }
  124.  
  125.        
  126.        
  127.         static void Main(string[] args)
  128.         {
  129.             _Main().Wait();
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement