获取递归嵌套文档 - Mongoose / Mongodb
我有一个递归嵌套模式,就像博客上的评论一样。将可能有几层深的单独嵌套文档拉出的最佳方法是什么?
据我所知,您可以获取根文档,然后深入到您想要的文档,但在递归情况下,所需文档的深度可能未知,我应该如何检索它。循环并执行 if 看看它是否正确......?
I have a recursively nested schema just like comments work on a blog. What is the best way to pull an individually nested document out which may be several layers deep.
I understand that you get the root document out, then drill down to the document you want, but in a recursive situation where the wanted document may be an unknown number of levels deep how should I retrive it. Loop through and do an if to see if its the correct one...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来没有猫鼬方法可以做到这一点,所以我使用像这样的递归查找函数来查找嵌套的文件夹:
ps这使用下划线库
looks like there is no mongoose way to do it so im using a recursive find function like this for finding a folder which is nested:
p.s. this uses the underscore library
快速回答是:您无法加载子文档,因为 mongodb 不支持它。在 mongodb 中,您可以仅加载根文档,然后在客户端(从任何深度级别)从中提取子文档。
Quick answer is: you can't load sub document, because mongodb does not support it. In mongodb you can load only root document and then extract from it sub document at client side (from any level of deep).