dbref - MongoDB DBReference how to? -
i'm learning mongodb , have next questions.
there mongodb documents
this 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 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 do, it's obtain "longitud" , "latitud" "coordenada" "numero" 20 of "rutas" document instance
how can this?
ps sorry spanish terms.
according mongodb site dbref, need use drivers unpack reference. don't think mongo shell can unpack you.
http://docs.mongodb.org/manual/reference/database-references/
to resolve dbrefs, application must perform additional queries return referenced documents. many drivers have helper methods form query dbref automatically. drivers [1] not automatically resolve dbrefs documents. dbrefs provide common format , type represent relationships among documents. dbref format provides common semantics representing links between documents if database must interact multiple frameworks , tools. unless have compelling reason use dbrefs, use manual references instead.
based on that, suggest change using manual reference (just document id) instead.
to answer question however, can use language drivers below example in python using pymongo:
from pymongo import mongoclient bson.objectid import objectid bson.dbref import dbref client = mongoclient() db = client.testdb rutas_20 = list(db.rutas.find({"numero": "20"})) ruta in rutas_20: 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 use db.dereference(dbref()) well.
hope helps. cheers.
Comments
Post a Comment