I'm writing a small companies profile management system on NodeJS. I use MongoDB for data storage. I have collection "companies", where every object is a single company. Cetainly, companies have several params, such as "name", "e-mail", "tel". These all are single string fields. Here's my current schema for "company"-object.
{
"company_name": "ACME Ltd.",
"company_email": "acmeltd@example.com",
"company_tel": "+49374727384",
"_id": {
"$oid": "4fd115b6e94a7e2408000001"
}
}
Here is my code to save current "company"-profiles:
app.post('/addcompany',function(req,res){
db.collection("companies",function(err,collection){
collection.insert( {
"company_name":req.body.company_name,
"company_email":req.body.company_email,
"company_tel":req.body.company_tel
});
});
res.redirect('/companies');
});
However, every company has several (different for every company) suppliers and I'd like to storage them in "company"-object. So, I think I should create inner object "suppliers" inside my "company"-object, and object "suppliers" has to have inner objects "supplier1", "supplier2", etc. Every "supplier1"-object should have 6 params such as "supplier_name", "supplier_tel", etc. So, my question is, which is the best way to storage supplies list in my "company"-object and how should I work with is from NodeJS?
Update: I've modified my code like this, and now it works!
app.post('/addcompany',function(req,res){
db.collection("companies",function(err,collection){
collection.insert( {
"company_name":req.body.company_name,
"company_email":req.body.company_email,
"company_tel":req.body.company_tel,
"company_suppliers" : [
{ "supplier_name" : "jane_test", "supplier_tel" : "123456_test", "comment" : "Good supplier_test" },
{ "supplier_name" : "jessy_test", "supplier_tel" : "654321_test", "comment" : "Bad supplier_test" }
]
});
});
res.redirect('/companies');
});
If you're set on storing them inside the company object, I'd recommend using the data structure meant for that purpose: arrays.
The discussion on MongoDB about schema design is prevalent to the decisions you are here making: http://www.mongodb.org/display/DOCS/Schema+Design In particular look at the "Embedding and Linking" section.