简体   繁体   中英

How to define Type for MongoDB

C# Project for a bakery chain to log every make including ingredient and actual recipe, etc. Seems a bit too much free-form for someone like me done lots of SQL. MongoDB came to my mind. Started the typical Type, database interface and Repository class, I then realized I'm so used to map out everything into a Type class to define each property/column. Reason I've chosen MongoDB is it can't be determined exactly how many columns. Sometimes they wrote in a certain order and items to the books, other times they'd just wrote totally free-form, random. It's impossible to pre-define them in Type. Of course, workaround is to make up 20 properties/columns as they'll never use this much. I know MongoDB also has schema. But, this way simply goes back to the usual fixed-schema SQL.

If MongoDB is still the database choice, C# might have the following options for Type:

  1. Capture everything into a big string (make and recipe)
Bakery{
int UserId, 
string UserName,
Datetime makeTime,
string make,
string recipe
}
  1. A bit more organized, drawback is it can't handle non-pair collection, for example horizontally 3+ fields.
Bakery{
    int UserId, 
    string UserName,
    Datetime makeTime,
    iCollection<make> makes,
    iCollection<recipe> recipes
    }

public class make
{
string fieldName,
string actualValue
}
public class recipe
{
string ingredientName,
string dose
}
  1. pre-define up to 20 fields per row/document, I'd rather go back to SQL.
Bakery{
    int UserId, 
    string UserName,
    Datetime makeTime,
    iCollection<make> makes,
    iCollection<recipe> recipes
    }

public class make
{
string fieldName1, string actualValue1,
...
string fieldName20, string actualValue20
}
public class recipe
{
string ingredientName1, string dose1,
...
string ingredientName20, string dose20
}

Must be able to query an item and value if exists. Need some expert advice please. Thank you.

You can simply make your makes and receipts as Dictionary<string,string> .

Please check this anwser as an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM