title: How to convert type from ObjectId to Long when using MongoTemplate?
body: <p>Normally when using spring data repositories, an object in which the result data can be stored is needed like the customer in this example: <a href="https://spring.io/guides/gs/accessing-data-mongodb/" rel="nofollow noreferrer">https://spring.io/guides/gs/accessing-data-mongodb/</a> . </p>
  
  <p>In my case I'm trying to use an object which is declared in another project I'm importing using maven - let's call it <code>MyDoc</code>. The object has an attribute <code>Long id</code> while the document in the MongoDB has an addition field <code>_id</code> from type <code>ObjectId</code>. This btw is like this because the MongoDB serves as an archive and the actual <code>id</code> from MyDoc would not be unique.</p>
  
  <p>In a service class I then use this the MongoTemplate to make database queries like this:</p>
  
  <pre><code>List&lt;MyDoc&gt; list = template.findAll(MyDoc.class, "DOCS");
  </code></pre>
  
  <pre><code>org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.bson.types.ObjectId] to type [java.lang.Long] 
  </code></pre>
  
  <p>How can I convert the ObjectId to a type of Long?</p>
  
  <p>Alternately, I could, of course, use the java MongoDB driver but I wanted to reduce the number of dependencies to maintain since the MongoDB driver comes with the boot-starter-data-MongoDB dependency, and also hoped for a more intuitive way, in the end, to interact with the database like with the spring data repositories.</p>