我使用 apollo server 通过 mongoose 连接 mongodb 进行后台开发。 现在遇到了一个问题,一个 collection Components 的结构为:
Components {
coreInformation {
reflowId: String
type: String
...
}
flowInformation {
owner: String
user: String
...
}
}
另一个 collection Vessels 的结构为:
Vessels {
imo: String
name: String
}
在 Components.flowInformation.user 中存储的是 Vessels.imo, 现在前端是列表显示 components,需要得到 Vessels.name,在传统的数据库中,有联合查询,很容易搞定。 但是在 mongoDB 中没有,我看网上说可以用 Populate+ Ref 搞定,但是我的 user 是包在 flowInformation 这个对象中的,我没有看到相关的例子。请 V 友帮忙,该如何使用,或者有其它的办法。谢谢。
PS:并不想把 name 作为冗余数据也放到 component 中,因为会改变原有 document 结构,改动太大。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.