0

I've got the following collections:

Table1 = { _id, Title }  
Table2 = { _id, Name }  
Table3 = { _id, TableType, TableId } 

I need to do a query to return a result that includes the Title or Name value from either Table1 or Table2 based on the "TableType" and "TableId" defined in Table3.

So, if I had the following data:

Table1 = {"_id":ObjectId("1"), "Title":"Title1"}  
Table2 = {"_id":ObjectId("2"), "Name":"Name1"}  
Table3 = {"_id":ObjectId("3"), "TableType":"Table1", "TableId":ObjectId("1")}, {"_id":ObjectId("4"), "TableType":"Table2", "TableId":ObjectId("2")}

The results should be:

Results = {"_id":ObjectId("3"), "TableId":ObjectId("1"), "Value":"Title1"},{"_id":ObjectId("4"), "TableId":ObjectId("2"), "Value":"Name1"}

Any example code on how to pull this off with the latest C# driver would be greatly appreciated. Please don't suggest that my data structure is wrong. I need it this way for a specific reason.

SoftwareGuy74
  • 63
  • 1
  • 4
  • If you ask for a solution and you state "don't suggest your data structure is wrong", then just drop this question and solve it yourself... **You're making a wrong assumption about MongoDB: it doesn't support a *join*: MongoDB as other database engines aren't relational database systems**. Thus, **if you're thinking about a join, your data structure is absolutely wrong. Period.** – Matías Fidemraizer Jan 24 '16 at 19:40
  • I'm voting to close this question as off-topic because the OP wants a magical solution negating the chance to provide an answer to build a better data structure in Mongo in order to solve the issue. – Matías Fidemraizer Jan 24 '16 at 19:41
  • Geeze, tough crowd. So, here's the deal. I don't want to store unnecessary data, so that is why I don't want to repeat the same data (Title, Name) from Table1 and Table2 in every record in Table3. Is that so unreasonable? BTW, isn't the very purpose of the aggregation framework in MongoDB to do just such a thing as I have asked for a solution on? Perhaps I didn't word my question right, but come on folks, is it really that hard to give me some pointers on how to solve this problem? – SoftwareGuy74 Jan 25 '16 at 03:28
  • You're right, you've asked the question in the wrong way... You ask for a solution based on your own assumptions and you close the door to a feasible solution. – Matías Fidemraizer Jan 25 '16 at 10:04

0 Answers0