MongoDBDBReference怎么来的?

MongoDB DBReference how to?

我正在学习 MongoDB,我有下一个问题。

有我的MongoDB个文件

这是 coordenada 文档

> db.coordenada.find().pretty()
{
        "_id" : ObjectId("5579b81342a31549b67ad00c"),
        "longitud" : "21.878382",
        "latitud" : "-102.277364"
}
{
        "_id" : ObjectId("5579b85542a31549b67ad00d"),
        "longitud" : "21.878626",
        "latitud" : "-102.280379"
}
{
        "_id" : ObjectId("5579b89442a31549b67ad00e"),
        "longitud" : "21.878845",
        "latitud" : "-102.283512"
}
{
        "_id" : ObjectId("5579b8bf42a31549b67ad00f"),
        "longitud" : "21.879253",
        "latitud" : "-102.286698"
}
{
        "_id" : ObjectId("5579b8dd42a31549b67ad010"),
        "longitud" : "21.879203",
        "latitud" : "-102.291558"
}
{
        "_id" : ObjectId("5579b8fd42a31549b67ad011"),
        "longitud" : "21.878427",
        "latitud" : "-102.296375"
}
{
        "_id" : ObjectId("5579b91d42a31549b67ad012"),
        "longitud" : "21.877571",
        "latitud" : "-102.299659"
}

这是 rutas 文档

> db.rutas.find().pretty()
{
        "_id" : "1",
        "nombre" : "Ruta Penal",
        "numero" : "20",
        "coordenadas" : [
                DBRef("coordenada", "5579b91d42a31549b67ad012")
        ]
}
{
        "_id" : "2",
        "nombre" : "Ruta Penal",
        "numero" : "20",
        "coordenadas" : [
                DBRef("coordenada", "5579b91d42a31549b67ad012")
        ]
}
{
        "_id" : "3",
        "nombre" : "Ruta Penal",
        "numero" : "20",
        "coordenadas" : [
                DBRef("coordenada", "5579b85542a31549b67ad00d")
        ]
}
{
        "_id" : 6,
        "nombre" : "Ruta Penal",
        "numero" : "20",
        "coordenadas" : [
                DBRef("coordenada", "5579b85542a31549b67ad00d")
        ]
}
>

我正在尝试做的是从 "coordenada" 但仅适用于 "numero" 20 of "rutas" 文档例如

我该怎么做?

PS 抱歉,西班牙语条款。

根据 DBRef 的 mongodb 站点,您需要使用驱动程序来解压引用。我认为 mongo shell 无法为您解压。

http://docs.mongodb.org/manual/reference/database-references/

To resolve DBRefs, your application must perform additional queries to return the referenced documents. Many drivers have helper methods that form the query for the DBRef automatically. The drivers [1] do not automatically resolve DBRefs into documents. DBRefs provide a common format and type to represent relationships among documents. The DBRef format also provides common semantics for representing links between documents if your database must interact with multiple frameworks and tools. Unless you have a compelling reason to use DBRefs, use manual references instead.

基于此,我建议改用手动参考(仅文档 ID)来更改它。

然而,要回答您的问题,您可以使用任何语言驱动程序,但下面是 Python 中使用 pymongo:

的示例
from pymongo import MongoClient
from bson.objectid import ObjectId
from bson.dbref import DBRef

client = MongoClient()
db = client.testDB

rutas_20 = list(db.rutas.find({"numero": "20"}))
for ruta in rutas_20:
    for coordenada in ruta.get('coordenada'):
        coord_doc = db.coordenada.find_one({"_id": ObjectId(coordenada.id) })
        print coord_doc.get('longitud'), coord_doc.get('latitud')

您也可以使用 db.dereference(DBRef())。

希望对您有所帮助。 干杯。

是的,你肯定可以通过引用另一个的对象id来获取特定项目的经纬度class。

要使用 momgo dbRef,您必须根据您使用的特定语言使用特定的驱动程序。驱动文档会告诉你可以使用的功能。

我使用 PHP,因此指的是, http://www.php.net/manual/en/class.mongodbref.php/