MongoDB DBReference how to?

2k views Asked by At

i'm learning MongoDB and i have the next questions.

There are my MongoDB documents

This is coordenada document

> 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"
}

And this is rutas document

> 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")
        ]
}
>

What i'm tryin to do, it's obtain the "longitud" and "latitud" from "coordenada" but only for the "numero" 20 of "rutas" document for instance

How can i do this?

PS sorry for the spanish terms.

2

There are 2 answers

0
Wan B. On BEST ANSWER

According to the mongodb site for DBRef, you need to use drivers to unpack reference. I don't think mongo shell can unpack it for you.

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.

Based on that, I would suggest to change it using manual reference (just the document id) instead.

To answer your question however, you can use any language drivers but below is an example in Python using 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')

You can also use db.dereference(DBRef()) as well.

Hope it helps. Cheers.

0
Soumojit Ghosh On

Yes, you can definitely obtain the latitude and longitude of the particular item by referencing the object id of the other class.

To use momgo dbRef you have to use the specific drivers depending on the particular language you are using. The driver documentation will tell you about the functions you can use.

I use PHP and hence refer to, http://www.php.net/manual/en/class.mongodbref.php/