title: How to resolve "No converter found capable of converting from type [org.bson.BsonUndefined] to type [java.lang.String] " only when Docs Count > 100
body: <p>I am using mongo db map reduce functionality for my requirement to process textual data.</p>
  
  <p>I am using the below map, reduce and finalize functions for my requirement. I am also using Spring Data Mongo DB to execute the mapreduce operation.</p>
  
  <p>The spring data mongo db code for the mapreduce functionality is executed on startup of the app by using the Spring Boot Application's, CommandLineRunner. The app is standalone non-web application.</p>
  
  <p>The <strong>Map function</strong> I use is as below,</p>
  
  <pre><code> function() {
      var document = this;
      var stopwords = ["the","be","to","of","and","a","in"];
      for(var prop in document) {
        if(prop === "_id" || typeof document[prop] !== 'string') {
          continue
        }
        (document[prop]).split(" ").forEach(
          function(word){
            var cleanedWord = word.replace(/[;,.]/g,"")
            if(
              stopwords.indexOf(cleanedWord) &gt; -1 ||
              !(isNaN(parseInt(cleanedWord))) ||
              !(isNaN(parseFloat(cleanedWord)))
            ) {
              return
            }
            var value={
                  userId:document.userId,
                  docId:document._id
            }
            emit(cleanedWord,value)
          }
        ) 
      }
    }
  </code></pre>
  
  <p><strong>The reduce function is:</strong></p>
  
  <pre><code>function(k,v){
     var output={'documents':[] };
     v.forEach(
        function(vs){
            var values = { 'userId': '', 'docIdStr': []};
           var checkExisting = false;
  
           output.documents.forEach(
                 function(test){
                            if (test.userId ===vs.userId)
                               {
                                  if(!(test.docIdStr.indexOf(vs.docId) &gt; -1))
                                    {
                                      test.docIdStr.push(vs.docId);
                                    }
                                   checkExisting =true;
                               }
                 }
           )
          if(!checkExisting)
                  {
                      values.userId=vs.userId ;
                      values.docIdStr.push(vs.docId);
                      output.documents.push(values);
                  }
        }
  
       )
       return output
    }
  </code></pre>
  
  <p><strong>Finalize function</strong></p>
  
  <pre><code>function(key,reducedValue){
  
          var finalValue = {documents:[]}
          if(reducedValue.documents) {
            finalValue.documents = reducedValue.documents.filter(
              function(item,pos,self){
                var loc = -1;
                for(var i=0;i&lt;self.length;i++){
                  if(self[i].valueOf() === item.valueOf()){
                    loc = i;
                    break
                  }
                }
                return loc === pos;
                }
            );
          } else {
  
                if (!reducedValue.documents)
                       {
                    var values = { 'userId': '', 'docIdStr': []};
  
                         values.userId=reducedValue.userId ;
                         values.docIdStr.push(reducedValue.docId.str);
                        finalValue.documents.push(values)
                       }
  
                else{
            finalValue.documents =reducedValue.documents 
                }
          }
  
          return finalValue
        }
  </code></pre>
  
  <p>The <strong>document structure in Source Collection</strong> is as below,</p>
  
  <pre><code>{
      "_id" : ObjectId("590af61465b4effc50b20961"),
      "userId" : "user9",
      "year" : 2016,
      "title" : "reprehenderit",
      "content" : "laborum ea consectetur fugiat duis velit consequat voluptate cillum culpa sit consectetur cillum"
  }
  </code></pre>
  
  <p>The sample <strong>document structure in target collection</strong> which I am trying to populate is as below. This is an access control list for a word mapping to user and doc list.</p>
  
  <pre><code>{
      "_id" : "Lorem",
      "value" : {
          "documents" : [ 
              {
                  "userId" : "user65",
                  "docIdStr" : [
                      ObjectId("590af61465b4effc50b20967"), 
                      ObjectId("590af61465b4effc50b20995"), 
                      ObjectId("590af61465b4effc50b209bd")
                              ]
              }
         ]
  }
  </code></pre>
  
  <p>The <strong>spring data mongodb Code</strong> fragment I use is as below,</p>
  
  <pre><code>mongoOperations.mapReduce
          (
  
               "CustomCollection",
               "classpath:map.js", 
               "classpath:reduce.js",
               new MapReduceOptions()
                  .finalizeFunction("classpath:finalize.js")
                  .outputCollection("CustomOutput"),
                  CustomOutput.class
          );
  </code></pre>
  
  <p>I am seeing exception in the logs when the count of docs in mongo source collection exceeds 100. With docs &lt;=100 it works fine without exceptions.</p>
  
  <p>Most of the fields in the populated collection are having undefined as the value of the fields.</p>
  
  <p>The exception stacktrace , I get is as below.</p>
  
  <pre><code>2017-05-04 15:35:10.154 ERROR 2556 --- [main] o.s.boot.SpringApplication               : Application startup failed
  
  java.lang.IllegalStateException: Failed to execute CommandLineRunner
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:785) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
      at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:772) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
      at com.demo.examples.MongoMapReduceApplication.main(MongoMapReduceApplication.java:20) [classes/:na]
  Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.bson.BsonUndefined] to type [java.lang.String]
      at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:324) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:206) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:187) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:823) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1199) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:79) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1145) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:871) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:284) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:322) ~[spring-data-commons-1.12.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:232) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:906) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1195) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:79) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1145) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:871) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:284) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:322) ~[spring-data-commons-1.12.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:232) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1197) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:79) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1145) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:871) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:284) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:322) ~[spring-data-commons-1.12.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:232) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:192) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:188) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:79) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2297) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate.mapReduce(MongoTemplate.java:1400) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate.mapReduce(MongoTemplate.java:1362) ~[spring-data-mongodb-1.9.8.RELEASE.jar:na]
      at com.demo.examples.service.MapReduceService.populateCollection(MapReduceService.java:21) ~[classes/:na]
      at com.demo.examples.MongoMapReduceApplication.run(MongoMapReduceApplication.java:25) [classes/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:801) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
      ... 4 common frames omitted
  
  2017-05-04 15:35:10.156  INFO 2556 --- [main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@235045: startup date [Thu May 04 15:35:08 IST 2017]; root of context hierarchy
  2017-05-04 15:35:10.158  INFO 2556 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
  2017-05-04 15:35:10.160  INFO 2556 --- [main] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:2, serverValue:148}] to localhost:27017 because the pool has been closed.
  </code></pre>
  
  <p>Can someone help in identifying the cause of the issue and resolving it.</p>