简体   繁体   English

Node js mongodb模块嵌入式文档

[英]Node js mongodb module embedded document

I am using node module "mongodb" and just started with the schema architecture for it. 我正在使用节点模块“ mongodb”,并且仅从其架构架构开始。 I read the documentation of mongodb module and found that there is no way I can store reference to another document inside a document. 我阅读了mongodb模块的文档,发现无法在文档中存储对另一个文档的引用。 I want to achieve something like below: 我想实现以下目标:

DocumentA
{
_id: ObjectID(123456789),
fieldA1: "test",
fieldA2: "test",
DocumentB: <ObjectId>
}

DocumentB
{
_id: ObjectID(09876543),
fieldB1: "test2",
fieldB2: "test2"
}

I want to query DocumentA by ObjectID(123456789) and fetch DocumentB also so that I can access the value if "fieldB2" & "fieldB1". 我想通过ObjectID(123456789)查询DocumentA,并同时获取DocumentB,以便可以访问“ fieldB2”和“ fieldB1”的值。 Is this possible to do in node js mongodb? 这可以在节点js mongodb中完成吗? If yes then please give me some knowledge or give me another solution(like, is querying twice for fetching inner object ok?) 如果是,那么请给我一些知识或给我另一个解决方案(例如,是否两次查询以获取内部对象好吗?)

Mongo does support the concept of references , but they are very weak references. Mongo确实支持引用的概念,但是它们是非常弱的引用。 There is no constraints on what your reference may be, so it may be totally invalid. 您所引用的内容不受任何限制,因此可能完全无效。

I think that there are 2 concepts that you're missing: 我认为您缺少2个概念:

  1. Mongo does not have the concept of 'joins'. Mongo没有“加入”的概念。 Its just not in their paradigm. 它只是不在他们的范例中。
  2. Instead Mongo favors embedding documents. 相反,Mongo倾向于嵌入文档。 So in your case, instead of having 2 documents, you can just have 因此,根据您的情况,您可以拥有2个文档,而不必拥有2个文档

.

DocumentA
{
_id: ObjectID(123456789),
fieldA1: "test",
fieldA2: "test",
DocumentB: {
    fieldB1: "test2",
    fieldB2: "test2"
    }
}

You can then just query for Document A and do whatever combinatoric logic you want with Document B because its right there with Document A 然后,您可以查询文档A,并对文档B进行所需的组合逻辑,因为它与文档A就在那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM