C# ile mongoDB işlemleri
private void button1_Click(object sender, EventArgs e)
{
ProductService productService = ProductService.ProductClientAsSingleton();
productService.ProductInsert();
//productService.ProductList();
//productService.ProductUpdate("5b17a2be59609e359032d21d");
//productService.ProductDelete("5b17a2be59609e359032d21d");
// productService.ProductListWhere(true);
//productService.ProductFind("5dc7128d4d6f992b08f29d18");
MessageBox.Show("tamam");
}
-----------------------------------------------ProductService.cs---------------------------------------------------------------------
using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
namespace Mongostock
{
public class ProductService
{
private static ProductService _client;
private static object lockObject = new object();
private MongoClient mongoClient;
private IMongoCollection<Root> productCollection;
private IMongoCollection<Stock> productCollectionItems;
public static ProductService ProductClientAsSingleton()
{
lock (lockObject)
{
return _client ?? (_client = new ProductService());
}
}
public ProductService()
{
mongoClient = new MongoClient("mongodb://user:pass@ipadresi/?authSource=admin");
var database = mongoClient.GetDatabase("databasename");
productCollectionItems = database.GetCollection<Stock>("stock_test");
}
public void ProductInsert()
{
/*
string json = @"{
'name' : 'Elma',
'price' : 11,
'quantity' : 3,
'status' : true,
'categoryid' : 1,
'IsActive' : true
}";
Product product = JsonConvert.DeserializeObject<Root>(json);
productCollection.InsertOne(product);
*/
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"c:\stock_all.json"))
{
JsonSerializer serializer = new JsonSerializer();
Root product = (Root)serializer.Deserialize(file, typeof(Root));
// Console.WriteLine("id: " + product.Stocks.ToString());
var stoklar = product.Stocks.ToList();
Console.WriteLine("Başlangıç: " + DateTime.Now);
productCollectionItems.InsertMany(stoklar);
/*
int say = 0;
string KayitDurumu = "YOK";
foreach (var stok in stoklar)
{
Console.WriteLine("say: " + say++);
Console.WriteLine("ps1: " + stok._id);
Console.WriteLine("ps2: " + stok.Ps2);
Console.WriteLine("ps3: " + stok.Ps3);
Console.WriteLine("ps4: " + stok.Ps4);
KayitDurumu=ProductFind(stok._id);
if (KayitDurumu == "YOK")
{
Stock eleman = new Stock
{
_id = stok._id,
Ps2 = stok.Ps2,
Ps3 = stok.Ps3,
Ps4 = stok.Ps4
};
productCollectionItems.InsertOne(eleman);
}
else
{
Stock eleman = new Stock
{
_id = stok._id,
Ps2 = stok.Ps2,
Ps3 = stok.Ps3,
Ps4 = stok.Ps4
};
ProductUpdate(stok._id,eleman);
}
}
Console.WriteLine("sayı: " + product.Stocks.Count);
*/
Console.WriteLine("Bitiş: " + DateTime.Now);
/*
int say = 0;
foreach (var stok in stoklar)
{
Console.WriteLine("say: " + say++);
Console.WriteLine("ps1: " + stok.Ps1);
Console.WriteLine("ps2: " + stok.Ps2);
Console.WriteLine("ps3: " + stok.Ps3);
Console.WriteLine("ps4: " + stok.Ps4);
Stock eleman = new Stock
{
Ps1 = stok.Ps1,
Ps2 = stok.Ps2,
Ps3 = stok.Ps3,
Ps4 = stok.Ps4
};
productCollectionItems.InsertOne(eleman);
}
Console.WriteLine("sayı: " + product.Stocks.Count);
*/
}
var database = mongoClient.GetDatabase("databasename");
database.DropCollection("stock");
database.RenameCollection("stock_test", "stock");
database.CreateCollection("stock_test");
/*
Product product = new Product
{
Name = "Armut",
Price = 11,
Quantity = 3,
Status = true,
CategoryId = 1,
Date = DateTime.Now,
isActive = true
};
productCollection.InsertOne(product);
*/
}
public void ProductList()
{
var products = productCollectionItems.AsQueryable<Stock>().ToList();
foreach (var product in products)
{
//Console.WriteLine("id: " + product.Id);
Console.WriteLine("ps1: " + product._id);
Console.WriteLine("ps2: " + product.Ps2);
Console.WriteLine("ps3: " + product.Ps3);
Console.WriteLine("ps4: " + product.Ps4);
}
}
public void ProductUpdate(string id,Stock eleman)
{
var result = productCollectionItems.UpdateOne(
Builders<Stock>.Filter.Eq("_id", ObjectId.Parse(id)),
Builders<Stock>.Update
.Set("ps2", eleman.Ps2)
.Set("ps3", eleman.Ps3)
.Set("ps4", eleman.Ps4)
);
}
public bool ProductDelete(string id)
{
var result = productCollection.DeleteOne(Builders<Root>.Filter.Eq("_id", ObjectId.Parse(id)));
return Convert.ToBoolean(result.DeletedCount);
}
public void ProductListWhere(bool status)
{
var products = productCollectionItems.AsQueryable<Stock>().Where(p => p.Status == status).ToList();
foreach (var product in products)
{
//Console.WriteLine("id: " + product.Id);
Console.WriteLine("ps1: " + product._id);
Console.WriteLine("ps2: " + product.Ps2);
Console.WriteLine("ps3: " + product.Ps3);
Console.WriteLine("ps4: " + product.Ps4);
}
}
public string ProductFind(string id)
{
var products = productCollectionItems.Find(Builders<Stock>.Filter.Eq("_id", id)).ToList();
var sayi = products.Count;
if (sayi > 0)
{
/* foreach (var product in products)
{
//Console.WriteLine("id: " + product.Id);
Console.WriteLine("ps1: " + product._id);
Console.WriteLine("ps2: " + product.Ps2);
Console.WriteLine("ps3: " + product.Ps3);
Console.WriteLine("ps4: " + product.Ps4);
}
*/
return id;
}
else
{
return "YOK";
Console.WriteLine("Durum: Bulamadım hacı abi");
}
}
}
}
-----------------------------------------------Stock.cs----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json;
namespace Mongostock
{
public class Stock
{
/* [BsonId]
public ObjectId Id { get; set; }*/
[JsonProperty(PropertyName = "ps1")]
public string _id { get; set; }
/*[BsonElement("ps1")]
public int Ps1 { get; set; }*/
[BsonElement("ps2")]
public string Ps2 { get; set; }
[BsonElement("ps3")]
public DateTime Ps3 { get; set; }
[BsonElement("ps4")]
public string Ps4 { get; set; }
[BsonElement("status")]
public bool Status { get; set; }
}
public class Root
{
//public int custId { get; set; }
//public int ordId { get; set; }
public List<Stock> Stocks { get; set; }
}
}
-----------------------------------------------stock_all.json---------------------------------------------------------------
{"Stocks":[{"ps1":100088,"ps2":2,"ps3":"2019-11-10T07:37:07.23Z","ps4":"9786053410300"},{"ps1":100089,"ps2":10,"ps3":"2019-11-10T07:37:07.23Z","ps4":"9786054525843"},{"ps1":100090,"ps2":21,"ps3":"2019-11-10T07:37:07.23Z","ps4":"9786054525317"},{"ps1":100091,"ps2":2,"ps3":"2019-11-10T07:37:07.23Z","ps4":"9786053410393"}]}
Hiç yorum yok :
Yorum Gönder