获取具有角子文档$ HTTP [英] Fetching subdocuments with angular $http
问题描述
说你有嵌套集合 A
, B
和 C
,它遵循如下图:
{收藏:一,
孩子:[{收藏:B
名:小蜜蜂,
孩子[{收藏:C,名:东欧}]}]}
这是 A1
,从MongoDB数据库与牵强 $ HTTP
:
{称号:标题A1
ID:A1,
小蜜蜂:[{ID:B1},{ID:B2}],
other_array:[{富:酒吧},{富:巴兹}]}
现在,在蜂
阵列,我们只引用( ID
)。我们要的是让下面的地图更新 A1
,并通过实际的数据替换引用。
这将意味着获取 B1
和 B2
从数据库中的数据,这可能各有 CEE
阵列,它的元素,我们需要从 C
收集来获取。
我想,人们可以很容易地创建一个专门的后台功能,将采取 A1
,做所有的提取一次,并返回最终结果;
,但你将如何通过使用多个$ HTTP / $资源调用get A1
的完全详细的版本?
如果一个递归函数中使用?
或者会是最好使用$ q和链的承诺?
如何行走地图(要知道哪些集合是相关的,他们的名字是什么),检索相关 B
项目,那么相关的 C
项目,并在最后更新 A1
,以替换 A1
喜欢的东西:
{称号:标题A1,ID:A1,蜜蜂:[{ID:B1,称号:称号B1,other_stuff:等等等等,东欧:[{ID:C1,称号:标题C1,c_specific:你好}]},{ID :B2,称号:标题B2,other_stuff:空,东欧:[]}],other_array:[{富:酒吧},{foo的: 巴兹}]}
进行多次HTTP调用来获取你需要会减慢你的网站的响应时间,并提供一个糟糕的用户体验一个值
块引用>所以我放弃了这个想法完全,并且将刚刚从数据库,其中已经包含了所有嵌套数组获取一个文档。
我看到的唯一的缺点是,当子文档直接修改包含子文档的所有文件必须手动修改。
Say you have nested collections
a
,b
, andc
, 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 updatea1
, and replace references by actual data.It would entail fetching
b1
andb2
data from database, which could each havecee
arrays, whose elements we would need to fetch from thec
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 relevantc
items, and at the very end updatea1
, so as to replacea1
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"}]}
解决方案As suggested in the comments by benjamin Gruenbaum,
making multiple http calls to fetch a single value you need will slow down your website's response time and provide a bad user experience
So I have dropped the idea altogether, and will just fetch one document from the database, which already contains all the nested arrays.
The only downside I see is that, when a subdocument is modified directly, all documents containing the subdocument will have to be manually modified.
这篇关于获取具有角子文档$ HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!