Say you have nested collections a
, b
, and c
, which follow the following map:
{"collection":"a",
"children":[{"collection":"b",
"name":"bee",
"children"[{"collection":"c","name":"cee"}]}]}
And here is a1
, fetched from a MongoDb database with $http
:
{"title":"title a1",
"id":"a1",
"bee":[{"id":"b1"},{"id":"b2"}],
"other_array":[{"foo":"bar"},{"foo":"baz"}]}
Right now, in the bee
array, we have only references (id
). What we want is to keep following the map to update a1
, and replace references by actual data.
It would entail fetching b1
and b2
data from database, which could each have cee
arrays, whose elements we would need to fetch from the c
collection.
I suppose one could easily create a dedicated backend function, that would take in a1
, do all the fetching at once and return the end result;
but how would you get the fully detailed version of a1
by using multiple $http/$resource calls?
Should a recursive function be used?
Or would it be best to use $q and chained promises?
How to walk the map (to know which collections are relevant, and what their name is), retrieve the relevant b
items, then the relevant c
items, and at the very end update a1
, so as to replace a1
with something like:
{"title":"title a1","id":"a1","bee":[{"id":"b1","title":"title b1","other_stuff":"blah blah","cee":[{"id":"c1","title":"title c1","c_specific":"hi there"}]},{"id":"b2","title":"title b2","other_stuff":null,"cee":[]}],"other_array":[{"foo":"bar"},{"foo":"baz"}]}
See Question&Answers more detail:
os