title: INNER JOIN collections in Spring Data MongoDB
body: <p><strong>How do I implement INNER JOIN in Spring Mongo?</strong></p>
  
  <p>Stupid sample just for example, it's actually incorrect, I just want to show <em>many-to-many</em> relation:</p>
  
  <pre><code>@Document(collection = "people")
  public class Person {
  
      @Id
      private String id;
      private String name;
      private String petId;
  
      // Getters, setters, constructors and etc.
  
   }
  
   @Document(collection = "pets")
   public class Pet {
  
      @Id
      private String id;
      private String name;
      private PetType petType; // Dog, Cat etc.
  
      // Getters, setters, constructors and etc.
  
   }
  </code></pre>
  
  <p>If I want to find all dogs that belong to John Smith, how should I do it? I need a query like this:</p>
  
  <pre><code>SELECT
       pt.*
  FROM
       pets AS pt INNER JOIN people AS pe ON (pt.id = pe.petId)
  WHERE
       pt.petType = ${input_petType}
       AND pe.name = ${input_name}
  </code></pre>
  
  <p>It means that I have <strong>two conditions</strong> in collection <code>Pet</code> and collection <code>Person</code>:</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.data.domain.Pageable;
  import org.springframework.data.mongodb.core.MongoTemplate;
  import org.springframework.data.mongodb.core.aggregation.Aggregation;
  import org.springframework.data.mongodb.core.aggregation.LookupOperation;
  import org.springframework.data.mongodb.core.query.Criteria;
  import org.springframework.data.mongodb.core.query.Query;
  
  import java.util.List;
  
  public interface PetRepository extends MongoRepository&lt;Pet, String&gt;, PetCustomRepository {
  
  }
  
  public interface PetCustomRepository {
  
       List&lt;Pet&gt; findAllByPetTypeAndPersonName(PetType type, String personName, Pageable pageable);
  
  }
  
  public class PetCustomRepositoryImpl implements PetCustomRepository {
  
      @Autowired
      private MongoTemplate mongoTemplate;
  
      @Override
      public List&lt;Pet&gt; findAllByPetTypeAndPersonName(PetType petType, String personName, Pageable pageable) {
          LookupOperation lookup = LookupOperation.newLookup()
                   .from("people")
                   .localField("_id")
                   .foreignField("petId")
                   .as("join_people");
          Aggregation aggregation = Aggregation.newAggregation(
                   Aggregation.match(Criteria.where("petType").is(petType)),
                   lookup,
                   Aggregation.match(Criteria.where("join_people.name").is(personName)),
                   Aggregation.skip(pageable.getPageNumber() * pageable.getPageSize()),
                   Aggregation.limit(pageable.getPageSize()));
          return mongoTemplate.aggregate(aggregation, Pet.class, Pet.class).getMappedResults();
      }
  
  }
  </code></pre>
  
  <p>The <code>findAllByPetTypeAndPersonName()</code> method returns empty list. What am I doing wrong?</p>