title: Spring Data MongoDB: set repository collection?
body: <p>how do I specify the collection I want my repository to be of?</p>
  
  <p>My configuration:</p>
  
  <pre><code>&lt;!-- Database --&gt;
  &lt;mongo:mongo id="mongoDb" host="localhost" port="27017"/&gt;
  &lt;mongo:db-factory id="mongoDbFactory" mongo-ref="mongoDb" dbname="test"/&gt;
  
  &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
      &lt;constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;mongo:repositories base-package="de.cochu.springtest.mongodb.repositories"/&gt;
  </code></pre>
  
  <p>My document:</p>
  
  <pre><code>@Document
  public class User {
  
      @Id String id;
      @Indexed String email;
  
      ...getters and setters omitted...
  
  }
  </code></pre>
  
  <p>the repository for the user-collection (is inside the ...mongodb.repositories package). I extended from the CrudRepository:</p>
  
  <pre><code>public interface UserRepository extends CrudRepository&lt;User, String&gt;{
      User findByEmail(String email);
  }
  </code></pre>
  
  <p>and finally, a controller of my spring servlet in which I want to use the UserRepository:</p>
  
  <pre><code>@Controller
  public class MyController {
  
      @Autowire UserRepository repo;
  
      @RequestMapping("/test.html")
      public String something(HttpServletRequest request) {
          User u = userRepository.findByEmail(request.getParameter("email"));
          ...
      }
  
  }
  </code></pre>
  
  <p>I set up some example users via console, the mongoTemplate.getCollection("users").count() works fine. The repository finds nothing.</p>
  
  <p>... how does the UserRepository know that I want to search the users in the collection "users" ?/Is this the right way to use the repositories?</p>
  
  <p>... Intellij IDEA does not recognize the autowiring of the UserRepository. Although it displays an error, it compiles and throws no exception. Is there something I can do about that?</p>