2

Say i have two different documents in difference collections: productand brand.

Product

{
     "_id" "527bf4cb41b8a817774d5124",
     "name": "Foo",
     "brand": "527bf4cb41b8a817774d5125"
}

Brand

{
     "_id" "527bf4cb41b8a817774d5125",
     "name": "Bar"
}

I want to automatically retrieve the brand document and embed it in the product document when i query the database. Obviously i can just query the database for the brand document after retrieving the product object, which would work fine. But can i do this in a smarter way, that does not require 2 queries? I can't seem to find a solution to this. Also i would like it to work if i find more than one document.

Edit:

I would like to end out with something like this:

{
    "_id" "527bf4cb41b8a817774d5124",
    "name": "Foo",
    "brand": {
        "_id" "527bf4cb41b8a817774d5125",
        "name": "Bar"
    }
}
nikora
  • 817
  • 1
  • 14
  • 29
  • With pymongo, I think you have to perform both queries. However, if you use an ODM, (eg: mongoengine, etc), you could use a ``ReferenceField`` that would do the job for you. For more information about mongoDB and database references, see http://docs.mongodb.org/manual/reference/database-references/#database-references – Balthazar Rouberol Dec 20 '13 at 09:43
  • MongoDB doesn't support joins, so you forced to make another queries. Alternatively, you can embed brand documents into product documents. I don't know other ways to design relations with MongoDB. – Artem Mezhenin Dec 20 '13 at 09:53
  • Also see http://stackoverflow.com/questions/4067197/mongodb-and-joins – shx2 Dec 20 '13 at 17:08

0 Answers0