Start a new topic

to-many relational data examples?

I've got a collection "A" with a 1-1 data relation to collection "B" which works nicely between native code and my business logic script. And A also has a to-many relationship to collection "C" called "c", which works 'fine' on the native side. I can save, fetch and even query on them. But I can't figure out how to access C's other fields in my BL script which has an A which it got by doing a .findOne() using the _id as the query. Is it failing because there's a (succeeding) asynchronous .update for that A (which is setting the b property)? I tried findAndModify, but didn't have much luck - instead of updating objects, I was wiping them out. (I cannot fix this by making it one relation on each C back to A, because each C has very many A's, but each A would only have 2 C's. Maybe in the future it has 3 or 4, but not 100.) I don't want to make 2 relations for the C's, one for each C, though if I find no solution here, I may fall back to that. I want to have one array of 2 C relations, which, as I said, works fine on the native side. But when I run my business script I query the property of A which points to the array of C's, and while it is an array (with keys 0 and 1), those keys' values print to the console as "0" and "1". I expected either C {_type, _id, _collection} at worst or full-on C objects at best. I want full-on C objects since they contain the data I want for the next portion of my script.

I tried:


var aCID = aC._id;

that gave me aCID == undefined.

I tried:

for(var d in a.c) {

for(var k in d) {'k: ' + k + ', = ' + d[k]); }}

which gave me:

"k: 0, = 1"

"k: 0, = 0"

So is there any sample code or guides or anything, really, on how to access relational data in BL scripts?

Between this and my post-fetch scripts not being called on relational objects (which removes some data for security purposes), I've had to move back to storing _id for my objects and fetching them during the script then denormalizing the data I'm going to need, and refetching the objects on the client side for any additional data needed.
Hello there Ben,

I'm not entirely sure how far along you are in your data modeling or how much you've architected your backend at this time. With that said it sounds like you've found a way to achieve the things you need to on your end.

In the event that you're still in the early stages of your application there's 2 articles that you may want to read that were outlined in our blog a while ago. The first is a bit more of an overview into how to model your data: . The second, and perhaps more important one to the topic at hand is . This will hopefully make sense when you read it, and will hopefully be able to apply it to your current design paradigm.

Please let me know if you have any other questions about this going forward,

Login or Signup to post a comment