title: How to use Mongo with Spring Data without ObjectId
body: <p>I have an existing mongo database in which ids (_id) are persisted as plain Strings.. This is sample data in Mongo DB:</p>
  
  <pre><code>{
      "_id" : "528bb0e2e4b0442f1479f1b4",
      "schoolId" : "URDLKYLFJXLWJGR193778316742298",
      "surname" : "Lewis",
      "admissionNumber" : "0347",
      "firstName" : "Martins"
  }
  </code></pre>
  
  <p>I have a Java object which is of the form:</p>
  
  <pre><code>public class Student {
  
  @Id
  private String id;
  private String schoolId;
  private String surname;
  private String admissionNumber;
  private String firstName;
  }
  </code></pre>
  
  <p>With Getters and Setters</p>
  
  <p>I also have a repository:</p>
  
  <pre><code>public interface StudentRepository extends MongoRepository&lt;Student, String&gt; {
  
      Student findOneBySurname(String surname);
      Student findOneById(String id);
      Student findOneBySurnameAndFirstName(String surname, String firstName);
  }
  </code></pre>
  
  <p>When I do a studentRepository.findAll() I am able to get all the Students in the database with all their fields populated correctly.</p>
  
  <p>When I do a studentRepository.findOne("528bb0e2e4b0442f1479f1b4") or a studentRepository.findOneById)"528bb0e2e4b0442f1479f1b4") it returns null</p>
  
  <p>When I do a debug of the mongo query I can see that it is calling:</p>
  
  <pre><code>2015-11-19 16:06:32.327 DEBUG 87081 --- [           main] o.s.data.mongodb.core.MongoTemplate      : findOne using query: { "id" : "528bb0e2e4b0442f1479f1b4"} fields: null for class: class com.ad.josh.domain.Student in collection: Student
      2015-11-19 16:06:32.331 DEBUG 87081 --- [           main] o.s.data.mongodb.core.MongoDbUtils       : Getting Mongo Database name=[joshdb]
      2015-11-19 16:06:32.333 DEBUG 87081 --- [           main] o.s.data.mongodb.core.MongoTemplate      : findOne using query: { "_id" : { "$oid" : "528bb0e2e4b0442f1479f1b4"}} in db.collection: josh.Student
  </code></pre>
  
  <p>Typically I can see that it is still querying with Object Id and not a plain String.</p>
  
  <p>I have seen suggestions to:</p>
  
  <p><a href="https://stackoverflow.com/questions/14329175/prevent-spring-data-for-mongo-to-convert-ids-to-objectid">Create a Converter which throws a RuntimeException</a> </p>
  
  <p>But this does not work; it just throws a RuntimeException. </p>
  
  <p>Any suggestions on how to use Spring Data to access a Mongo database where the Ids are defined as String already (in an existing database) would be really appreciated.</p>