[CSHARP-2446] Mapreduce failed for C# driver using MongoDB Server 4.0.4 Created: 05/Dec/18  Updated: 05/Apr/19  Resolved: 06/Dec/18

Status: Closed
Project: C# Driver
Component/s: Read Operations
Affects Version/s: 2.7.2
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Manikandan Kumaresan Assignee: Unassigned
Resolution: Done Votes: 0
Labels: mapreduce, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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;
}");



 Comments   
Comment by Jeffrey Yemin [ 06/Dec/18 ]

Hi manikandan24may@gmail.com from the error message that the server is replying with, it appears that there is a syntax error in the Javascript code that you're passing to the MapReduce helper method.

Just to let you know this project is for .NET driver bugs or feature requests. The best place for questions regarding MongoDB usage or the .NET driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a broader audience there. If your business requires an answer from MongoDB within a time frame then we do offer production support.

Regards,
Jeff

Generated at Wed Feb 07 21:42:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.