Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2446

Mapreduce failed for C# driver using MongoDB Server 4.0.4

      Map reduce failed for the MongoDB Enterprise server version 4.0.4 and i am using mongocsharpdriver version 2.7.2.

      When we do the mapreduce code it gets failed and throw exception as "Command mapreduce failed: SyntaxError: missing ) in parenthetical @:23:0".

      C# Code sample:**

      
      MongoClient client = new MongoClient(GetMongoSettings());
      IMongoDatabase db = client.GetDatabase("test");
      IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("collection");
      BsonJavaScript map = new BsonJavaScript(MapJs);
      BsonJavaScript reduce = new BsonJavaScript(ReduceJS);
      FilterDefinitionBuilder<BsonDocument> filterBuilder = new FilterDefinitionBuilder<BsonDocument>();
      FilterDefinition<BsonDocument> filter = filterBuilder.Empty;
      MapReduceOptions<BsonDocument, BsonDocument> options = new MapReduceOptions<BsonDocument, BsonDocument>
      {
           Filter = filter,
           MaxTime = TimeSpan.FromMinutes(1),
           OutputOptions = MapReduceOutputOptions.Reduce("Result", nonAtomic: true),
           Verbose = true
      };
      try
      {
           var mapredResult = collection.MapReduce(map, reduce, options).ToList();
      }
      catch (Exception ex)
      {
           Console.WriteLine($"Exception occurred {ex.Message}");
      }
      

      Map-Reduce commands:

      var mapper = new BsonJavaScript(@"function()
      {
       for (var columns in this)
       {
       if (!this[columns] || Object.prototype.toString.call(this[columns]) == '[object BSON]')
       getNestedColumns(columns, this[columns]);
       else
       emit(columns, this[columns]);
       }
      function getNestedColumns(rootColumn, embeddedDocument)
       {
       try
       {
       for (var nestedColumn in embeddedDocument)
       {
       var columnName = rootColumn + '__' + nestedColumn;
       if (!embeddedDocument[nestedColumn] && [typeof embeddedDocument[nestedColumn]].toString() == 'object' && embeddedDocument[nestedColumn] != '[object BSON]')
       {
       emit(columnName, embeddedDocument[nestedColumn]);
       }
       else if (!embeddedDocument[nestedColumn] && isObject(embeddedDocument[nestedColumn]))
       {
       getNestedColumns(columnName, embeddedDocument[nestedColumn]);
       }
       else
       {
       emit(columnName, embeddedDocument[nestedColumn]);
       }
       }
       }
       catch (error)
       {}
       }
      }");
      
      var reducer = new BsonJavaScript(@"function(key, value)
      {
          if (value[0] != null)
          {
              var integerValue = TryParseInt(value[0].toString(), null);
              if (integerValue !== null)
                  return integerValue;
              var floatValue = TryParseFloat(value[0].toString(), null);
              if (floatValue !== null)
                  return floatValue;
              var dateTimeValue = TryParseDateTime(value[0].toString(), null);
              if (dateTimeValue !== null)
                  return dateTimeValue;
              var booleanValue = TryParseBoolean(value[0].toString(), null);
              if (booleanValue !== null)
                  return booleanValue;
              return value[0].toString();
          }
          else
              return '';
      }function TryParseInt(str, defaultValue)
      {
          var retValue = defaultValue;
          if (str !== null)
          {
              if (str.length > 0)
              {
                  if (!isNaN(str))
                  {
                      retValue = parseInt(str);
                  }
              }
          }
          return retValue;
      }function TryParseFloat(str, defaultValue)
      {
          var retValue = defaultValue;
          if (str !== null)
          {
              if (str.length > 0)
              {
                  if (!isNaN(str))
                  {
                      retValue = parseFloat(str);
                  }
              }
          }
          return retValue;
      }function TryParseDateTime(str, defaultValue)
      {
          var retValue = defaultValue;
          if (str !== null)
          {
              if (str.length > 0)
              {
                  var d = new Date(str);
                  if (d != 'Invalid Date')
                      return d;
                  else
                      return null;
              }
          }
          return retValue;
      }function TryParseBoolean(str, defaultValue)
      {
          var retValue = defaultValue;
          if (str !== null)
          {
              if (str.length > 0)
              {
                  switch (str)
                  {
                      case true:
                      case 'true':
                          return true;
                      case 'false':
                      case false:
                          return false;
                      default:
                          return null;
                  }
              }
          }
          return retValue;
      }");

            Assignee:
            Unassigned Unassigned
            Reporter:
            manikandan24may@gmail.com Manikandan Kumaresan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: