0

I'm trying to make queries with MongoDB but i have a questions.

Query and structure: enter image description here

How do I view information about the author at that job with this query? (for example I want to see the name of the autor with the information of the job)

sigLosco
  • 101
  • 3
  • 5
  • 10

1 Answers1

1

Mongo does not support joins as in relational databases. You'll need to execute a second query to get the autore data.

var opera = db.tabella2.findOne();

var autore = db[opera.autore.$ref].find({id:opera.autore.$id});

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

EDIT:

Sometimes you'll want to embed documents to have a better data model. If you have a 1-to-n relation between documents in tabella2 and tabella1, you can have a single tabella collection with documents like this, for example:

{
  "_id": 1,
  "nome": "Matteo",
  "cognome": "Cappella",
  "opere": [
    {
      "_id": 1,
      "titolo": "Eppoi",
      "categoria": "back-end",
    }
  ]
}

Read more at http://docs.mongodb.org/manual/core/data-model-design/

ericbn
  • 10,163
  • 3
  • 47
  • 55
  • Additional information: Because MongoDB can not resolve references on the database, they are a lot less practical than they seem at first glance. In most situations it is better to either use just a value of an unique identifier from the other collection (_id is good but when you have a more readable identifier, you can also use that) or embed the referenced document. – Philipp Jul 30 '15 at 13:30