3

http://docs.mongodb.org/manual/reference/database-references/#DatabaseReferences-SimpleDirect%2FManualLinking

对于几乎所有要存储两个文档之间的关系的情况,请使用手动参考。引用很容易创建,您的应用程序可以根据需要解析引用。

正如 mongodb 参考文档中所指出的那样,使用手动链接/引用而不是像这样使用 DBRef 似乎更合理:

stories : [{ type: Schema.ObjectId, ref: 'Story' }]

通过 DBref 实现关系看起来非常简单。除此之外,我找不到关于如何在模式中最有效地实现手动引用的可靠资源。建议:

stories : [{ type: Schema.ObjectId}] OR
stories : [{ type: Number] OR
stories : [{ type: String]

手动参考应该如何实现?也将非常感谢插入的示例。

4

1 回答 1

0

实现这将取决于您在哪个环境中使用哪个库。

这是 node.js 中猫鼬的一个很好的例子: https ://mongoosejs.com/docs/populate.html

于 2021-02-26T17:01:49.167 回答