Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Requires official C# and .NET MongoDB Driver 2.5+
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using System.Threading.Tasks;
- using MongoDB.Bson;
- using MongoDB.Driver;
- namespace ConsoleApp1
- {
- class Program
- {
- static async Task _Main()
- {
- IMongoClient client = new MongoClient("mongodb://localhost:27017/");
- IMongoDatabase database = client.GetDatabase("EmsIssuing");
- //IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("BalanceHistory");
- // Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/
- var fields = new HashSet<string>();
- fields.Add("_id");
- fields.Add("When");
- fields.Add("WalletId");
- fields.Add("CardId");
- fields.Add("MaskedPan");
- fields.Add("CardholderName");
- fields.Add("Description");
- fields.Add("TransactionType");
- fields.Add("LifeCycle");
- fields.Add("Source");
- fields.Add("TransactionRegion");
- fields.Add("Currency");
- fields.Add("Amount");
- fields.Add("Fee");
- fields.Add("AvailableBalance");
- fields.Add("LedgerBalance");
- // Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/
- //var filter = RunFilter();
- int fromDate = 1, toDate = 2;
- GenerateCSV();
- void GenerateCSV()
- {
- var result = database.GetCollection<BsonDocument>("BalanceHistory").Find(RunFilter(31,1));
- var csv = new StringBuilder();
- string headerLine = string.Join(",", fields);
- csv.AppendLine(headerLine);
- foreach (var element in result.ToListAsync().Result)
- {
- string line = null;
- var counter = 0;
- foreach (var field in fields)
- {
- counter++;
- BsonValue value;
- try
- {
- value = element.GetElement(field).Value;
- }
- catch (Exception e)
- {
- //todo add logging here
- line = line + ",";
- continue;
- }
- // Example deserialize to string
- switch (value.BsonType)
- {
- case BsonType.ObjectId:
- case BsonType.String:
- case BsonType.DateTime:
- line += value;
- break;
- case BsonType.Int32:
- line += value.AsInt32;
- break;
- case BsonType.Int64:
- line += value.AsInt64;
- break;
- }
- if (counter != fields.Count)
- {
- line += ",";
- }
- }
- csv.AppendLine(line);
- }
- var path = $"D:\\Temp\\Reports\\31";
- Directory.CreateDirectory(path);
- File.WriteAllText($"{path}\\EmsIssuing.BalanceHistory.csv", csv.ToString());
- }
- }
- public static BsonDocument RunFilter(int fromDate, int ToDate)
- {
- BsonDocument filter = new BsonDocument();
- filter.Add("$and", new BsonArray()
- .Add(new BsonDocument()
- .Add("When", new BsonDocument()
- .Add("$gte", new BsonDateTime(DateTime.ParseExact($"2019-07-{fromDate:D2} 01:00:00.000+0200", "yyyy-MM-dd HH:mm:ss.fffzzz", CultureInfo.InvariantCulture)))
- )
- )
- .Add(new BsonDocument()
- .Add("When", new BsonDocument()
- .Add("$lt", new BsonDateTime(DateTime.ParseExact($"2019-08-{ToDate:D2} 01:00:00.000+0200", "yyyy-MM-dd HH:mm:ss.fffzzz", CultureInfo.InvariantCulture)))
- )
- )
- );
- return filter;
- }
- static void Main(string[] args)
- {
- _Main().Wait();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement