title: How to query by joining 2 documents based on the _id in MongoDB?
body: <p>I have a data model like this picture below and all of them are in the same collection, but of course are different in data structure. I used the manual references in document movies and document studios. For example, like the code below:</p>
  <ul>
  <li><p>For document people</p>
  <p>{
  &quot;_id&quot;: 100600,
  &quot;first_name&quot;: &quot;Becka&quot;,
  &quot;last_name&quot;: &quot;Battson&quot;,
  &quot;birth_day&quot;: &quot;2001-10-03&quot;
  }</p>
  </li>
  <li><p>For document movies</p>
  <p>{
  &quot;_id&quot;: 1100,
  &quot;title&quot;: &quot;Tom and Jerry&quot;,
  &quot;director_id&quot;: 100100,
  &quot;release_year&quot;: 2018,
  &quot;imdb&quot;: {
  &quot;rating&quot;: 4.8,
  &quot;votes&quot;: 100
  },
  &quot;actors&quot;: [
  {
  &quot;person_id&quot;: 100300,
  &quot;as&quot;: &quot;Tom&quot;
  },
  {
  &quot;person_id&quot;: 100400,
  &quot;as&quot;: &quot;Jerry&quot;
  },
  {
  &quot;person_id&quot;: 100500,
  &quot;as&quot;: &quot;Nibbles&quot;
  }
  ]
  }</p>
  </li>
  <li><p>For document studios:</p>
  <p>{
  &quot;_id&quot;: 9991000,
  &quot;name&quot;: &quot;Walt Disney&quot;,
  &quot;year_founded&quot;: 1923,
  &quot;movies&quot;: [
  1100,
  1200
  ],
  &quot;headquarters&quot;: {
  &quot;address&quot;: &quot;1375 E Buena Vista Dr&quot;,
  &quot;city&quot;: &quot;New York&quot;,
  &quot;state&quot;: &quot;New York&quot;,
  &quot;country&quot;: &quot;US&quot;
  }
  }</p>
  </li>
  </ul>
  <p>I have some tasks that need join between 2 documents together to get data. For example, <em><strong>&quot;Indicates that the movies have the actor has the last name &quot;Battson&quot;</strong></em> . But I know that MongoDB doesn't support join documents like joining 2 tables in RDBMS.</p>
  <p>I have tried to used this code below in MongoDB shell but it doesn't make sense. I think I need to get the values from the _id in the result query, then push them into the array array_idActor so that the second code can run:</p>
  <pre><code>const array_idActor = db.getCollection('movies_subject').find({last_name: /Battson/})
  db.getCollection('movies_subject').find({actors:{$elemMatch:{person_id:{$in:array_idActor}}}}).pretty()
  </code></pre>
  <p>Please help me to fix this. Thank you so much.</p>