Database References数据库参考

On this page本页内容

For many use cases in MongoDB, the denormalized data model where related data is stored within a single document will be optimal. 对于MongoDB中的许多用例,将相关数据存储在单个文档中的非规范化数据模型将是最佳的。However, in some cases, it makes sense to store related information in separate documents, typically in different collections or databases.然而,在某些情况下,将相关信息存储在单独的文档中是有意义的,通常存储在不同的集合或数据库中。

Important重要

MongoDB 3.2 introduces $lookup pipeline stage to perform a left outer join to an unsharded collection in the same database. MongoDB 3.2引入了$lookup管道阶段,用于对同一数据库中的非共享集合执行左外部联接。For more information and examples, see $lookup.有关更多信息和示例,请参阅$lookup

Starting in MongoDB 3.4, you can also use $graphLookup pipeline stage to join an unsharded collection to perform recursive search. 从MongoDB 3.4开始,您还可以使用$graphLookup管道阶段加入非共享集合以执行递归搜索。For more information and examples, see $graphLookup.有关更多信息和示例,请参阅$graphLookup

This page outlines alternative procedures that predate the $lookup and $graphLookup pipeline stages本页概述了$lookup$graphLookup管道阶段之前的替代过程.

MongoDB applications use one of two methods for relating documents:MongoDB应用程序使用两种方法之一来关联文档:

Unless you have a compelling reason to use DBRefs, use manual references instead.除非您有充分的理由使用DBRefs,否则请使用手动引用。

[1] Some community supported drivers may have alternate behavior and may resolve a DBRef into a document automatically.一些社区支持的驱动程序可能具有替代行为,并可能自动将DBRef解析为文档。

Manual References手册参考

Background出身背景

Using manual references is the practice of including one document's _id field in another document. 使用手动引用是将一个文档_id字段包含在另一个文档中的做法。The application can then issue a second query to resolve the referenced fields as needed.然后,应用程序可以根据需要发出第二个查询以解析引用的字段。

Process

Consider the following operation to insert two documents, using the _id field of the first document as a reference in the second document:考虑以下操作以插入两个文档,使用第一个文档的_id字段作为第二个文档中的引用:

original_id = ObjectId()
db.places.insertOne({
    "_id": original_id,
    "name": "Broadway Center",
    "url": "bc.example.net"
})
db.people.insertOne({
    "name": "Erin",
    "places_id": original_id,
    "url":  "bc.example.net/Erin"
})

Then, when a query returns the document from the people collection you can, if needed, make a second query for the document referenced by the places_id field in the places collection.然后,当查询从people集合返回文档时,如果需要,您可以对places集合中的places_id字段引用的文档进行第二次查询。

Use

For nearly every case where you want to store a relationship between two documents, use manual references. 对于几乎所有要存储两个文档之间关系的情况,请使用手动引用The references are simple to create and your application can resolve references as needed.引用易于创建,应用程序可以根据需要解析引用。

The only limitation of manual linking is that these references do not convey the database and collection names. 手动链接的唯一限制是这些引用不传递数据库和集合名称。If you have documents in a single collection that relate to documents in more than one collection, you may need to consider using DBRefs.如果在一个集合中有与多个集合中的文档相关的文档,则可能需要考虑使用DBRefs。

DBRefs

Background出身背景

DBRefs are a convention for representing a document, rather than a specific reference type. DBREF是表示文档的约定,而不是特定的引用类型。They include the name of the collection, and in some cases the database name, in addition to the value from the _id field.它们包括集合的名称,在某些情况下还包括数据库名称,以及_id字段中的值。

Format总体安排

DBRefs have the following fields:DBREF具有以下字段:

$ref

The $ref field holds the name of the collection where the referenced document resides.$ref字段保存引用文档所在集合的名称。

$id

The $id field contains the value of the _id field in the referenced document.$id字段包含引用文档中_id字段的值。

$db

Optional.可选。

Contains the name of the database where the referenced document resides.包含引用文档所在的数据库的名称。

Only some drivers support $db references.只有一些驱动程序支持$db引用。

Example示例

DBRef documents resemble the following document:DBRef文档类似于以下文档:

{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }

Consider a document from a collection that stored a DBRef in a creator field:考虑在creator字段中存储DBRef的集合中的文档:

{
  "_id" : ObjectId("5126bbf64aed4daf9e2ab771"),
  // .. application fields
  "creator" : {
                  "$ref" : "creators",
                  "$id" : ObjectId("5126bc054aed4daf9e2ab772"),
                  "$db" : "users"
               }
}

The DBRef in this example points to a document in the creators collection of the users database that has ObjectId("5126bc054aed4daf9e2ab772") in its _id field.本例中的DBRef指向users数据库的creators集合中的一个文档,该文档的_id字段中有ObjectId("5126bc054aed4daf9e2ab772")

Note注意

The order of fields in the DBRef matters, and you must use the above sequence when using a DBRef.DBRef中字段的顺序很重要,使用DBRef时必须使用上述顺序。

Driver Support for DBRefsDBRefs的驱动程序支持

DriverDBRef SupportNotes备注
CNot SupportedYou can traverse references manually.您可以手动遍历引用。
C++Not SupportedYou can traverse references manually.您可以手动遍历引用。
C#SupportedPlease see the C# driver page for more information.有关更多信息,请参阅C#驱动程序页面。
GoNot SupportedYou can traverse references manually.您可以手动遍历引用。
HaskellNot SupportedYou can traverse references manually.您可以手动遍历引用。
JavaSupportedPlease see the Java driver page for more information.有关更多信息,请参阅Java驱动程序页面
Node.jsSupportedPlease see the Node.js driver page for more information.有关更多信息,请参阅Node.js驱动程序页面
PerlSupportedPlease see the Perl driver page for more information.有关更多信息,请参阅Perl驱动程序页面
PHPNot SupportedYou can traverse references manually.您可以手动遍历引用。
PythonSupportedPlease see the PyMongo driver page for more information.有关更多信息,请参阅PyMongo驱动程序页面
RubySupportedPlease see the Ruby driver page for more information.有关更多信息,请参阅Ruby驱动程序页面
ScalaNot SupportedYou can traverse references manually.您可以手动遍历引用。

Use

In most cases you should use the manual reference method for connecting two or more related documents. 在大多数情况下,您应该使用手动引用方法来连接两个或多个相关文档。However, if you need to reference documents from multiple collections, consider using DBRefs.但是,如果需要引用多个集合中的文档,请考虑使用DBRefs。

←  Data Model ReferenceTransactions →