I came across database references: http://docs.mongodb.org/manual/reference/database-references/ and had some questions about how exactly to link documents together.
Assume we have two db.categories and db.products collections... I have documents in db.products that need to link to their categories in db.categories.
I have a "category" field in db.products that I will
store the objectID of the category document in. However, I do not see a
way to specify that this objectID references the db.categories collection.
Do I need to use DBREFs?
Below is a brief example of my data...
Document in db.categories:
{
"_id" : ObjectId("546b72a47ea1d03593000158"),
"name" : "Category1",
"type" : "type1 of category1",
"spec_columns" : [
{
"field" : "capacity",
"display" : "Capacity (tons)"
},
{
"field" : "weight",
"display" : "Weight (lbs)"
}
]
}
Document in db.products:
{
"_id" : ObjectId("546a575f7ea1d03593000154"),
"model" : "NER003H",
"category" : ObjectId("546b72a47ea1d03593000158"),
"capacity" : "1/2",
"weight" : 10
}
Any help, suggestions and advice is welcome and greatly appreciated.
Darius
db.products.find({speed_direct_fpm:{$gt:"20"}, category: "546ce5087ea1d0359300015c"});
// $category['spec_columns'] - http://pastebin.com/TEkqCEGn
// $equipment_list - http://pastebin.com/408ntdFp
echo "<table border='1'>"; echo "<thead><tr>"; foreach ($category['spec_columns'] as $columnID=>$columnData) { if (!isset($columnData['field']) && isset($columnData['children'])) { echo "<th colspan='".count($columnData['children'])."' align='center'>".$columnData['display']."</th>"; } else { echo "<th>".$columnData['display']."</th>"; } } echo "</tr></thead>"; // Second loop: echo "<tbody>"; foreach($equipment_list as $equipment) { echo "<tr>"; foreach ($category['spec_columns'] as $columnData) { echo "<td><a href='index.php?action=view_equipment&id=".$equipment['_id']."'>".$equipment[$columnData['field']]."</a></td>"; } echo "</tr>"; } echo "</tbody>"; echo "</table>";