I'm playing around with MongoDB and trying to write some JSON documents to MongoDB. Currently, I am hard coding the values and outputting to the screen. The end goal is to migrate data from SQL Server to MongoDB, but until I can get the initial Class working correctly (hard coded method), I can't really proceed.
Let's say I have some financial securities and tickers. Currently, in SQL Server, these are in seperate tables (dbo.Security and dbo.Ticker) with SecurityId being the PK/FK.
I have added a few documents manually, using this JSON document:
{"SecurityId": 1234,"CompanyId": 1,"SenderId": 2,"TradingAccountId": 1,"InstrumentDescription": "VODAFONE","InstrumentTypeId": 5,"ImportId": 1,"Ticker" : [ { "TickerType" : "CUSTOM", "TickerValue" : "VODA1" }, { "TickerType" : "ISIN", "TickerValue" : "XS123456789" } ] }
This works fine. I can run this in the MongoDB shell and add a document. Initially, I created a single class that did not represent any Ticker data and that worked great. Now I wanted to save the tickers with the security, so I googled
around and stumbled across a List which seemed to fit the bill, but I can't figure this part of the class out.
Here is what I came up with (mostly via Google searches). I've excluded the include, namespace parts of the .cs files.
Ticker Class (Ticker.cs file):
class Ticker { public string TickerType { get; set; } public string TickerValue { get; set; } public bool Default { get; set; } }
Security Class (Security.cs file):
class Security { public int InstrumentId { get; set; } public int CompanyId { get; set; } public int SenderId { get; set; } public int TradingAccountId { get; set; } public string InstrumentDescription { get; set; } public int InstrumentTypeId { get; set; } public int ImportId { get; set; } public List<Ticker> SecurityTicker { get; set; } }
Form1.cs:
Security sec = new Security { InstrumentId = 1, CompanyId = 1, SenderId = 1, TradingAccountId = 1, InstrumentDescription = "ABCD", InstrumentTypeId = 1, ImportId = 1 }; string json = JsonConvert.SerializeObject(sec, Formatting.Indented);
What I can't figure out is what code is needed to write data into the SecurityTicker List. This could be no tickers or multiple tickers.
Initially, I figured something like:
SecurityTicker.Add........
But all I get is a tool tip with "Invalid initializer member declarator".
Now, I am stuck. To be honest, I don't even know if this is the correct approach to load data into a class and then serialize it into a JSON document.
Any advice is welcome! If you need more information, please let me know.
Thanks!