public static void InsertDataMonogDB() { var connectionStr = "mongodb://localhost"; var client = new MongoClient(connectionStr); var server = client.GetServer(); var db = server.GetDatabase("supermarketExtra"); //Insert Vendor var expenses = db.GetCollection<MongoVendorExpense>(vendorCollectionString); var obj = new MongoVendorExpense() { VendorName = "Svoge", Month = 3, Year = 1998, Amount = 200 }; expenses.Insert<MongoVendorExpense>(obj); //Insert products reports var prodReport = db.GetCollection<MongoProductReport>(productColelctionString); var bira1 = new MongoProductReport() { ProductId = 3, ProductName = "Zagorka", VendorName = "Svoge", TotalIncomes = 234, TotalQuantitySold = 14 }; var bira2 = new MongoProductReport() { ProductId = 2, ProductName = "Kamenitza", VendorName = "Svoge", TotalIncomes = 334, TotalQuantitySold = 14 }; var bira3 = new MongoProductReport() { ProductId = 1, ProductName = "Amstel", VendorName = "Svoge", TotalIncomes = 14, TotalQuantitySold = 14 }; var bira4 = new MongoProductReport() { ProductId = 5, ProductName = "Korola", VendorName = "Svoge", TotalIncomes = 2234, TotalQuantitySold = 14 }; prodReport.Insert<MongoProductReport>(bira1); prodReport.Insert<MongoProductReport>(bira2); prodReport.Insert<MongoProductReport>(bira3); prodReport.Insert<MongoProductReport>(bira4); }
public static void GenerateProductReports(string folderName) { DirectoryInfo directory = Directory.CreateDirectory(folderName); List<MongoProductReport> mongoProductsReports = new List<MongoProductReport>(); MongoDBManager<MongoProductReport> mongo = new MongoDBManager<MongoProductReport>(); using (var msSQLServerContext = new SupermarketEntities()) { var sales = from product in msSQLServerContext.Products join vendor in msSQLServerContext.Vendors on product.VendorId equals vendor.VendorId join sale in msSQLServerContext.Sales on product.ProductId equals sale.ProductId group sale by sale.ProductId into productsById select new { ProductId = productsById.Key, SalesTotalQuantity = productsById.Sum(s => s.ProductQuantity), SalesTotalSum = productsById.Sum(s => s.ProductTotalSum) }; var productReports = from sale in sales join product in msSQLServerContext.Products on sale.ProductId equals product.ProductId join vendor in msSQLServerContext.Vendors on product.VendorId equals vendor.VendorId select new { ProductId = sale.ProductId, ProductName = product.ProductName, VendorName = vendor.VendorName, SalesTotalQuantity = sale.SalesTotalQuantity, SalesTotalSum = sale.SalesTotalSum }; foreach (var productReport in productReports) { JObject jsonObject = new JObject( new JProperty("product-id", productReport.ProductId), new JProperty("product-name", productReport.ProductName), new JProperty("vendor-name", productReport.VendorName), new JProperty("total-quantity-sold", productReport.SalesTotalQuantity), new JProperty("total-incomes", productReport.SalesTotalSum)); string filePath = Path.Combine(folderName, string.Format("{0}.json", productReport.ProductId)); using (FileStream file = File.Create(filePath)) { using (StreamWriter writer = new StreamWriter(file)) { writer.Write(jsonObject.ToString()); } } MongoProductReport mongoProductReport = new MongoProductReport { ProductId = productReport.ProductId, ProductName = productReport.ProductName, VendorName = productReport.VendorName, TotalQuantitySold = productReport.SalesTotalQuantity, TotalIncomes = productReport.SalesTotalSum }; mongoProductsReports.Add(mongoProductReport); } } mongo.InsertInMongoDB(mongoProductsReports, "ProductsReports"); }