KennyPowers
KennyPowers

Reputation: 5015

Mongodb Aggregation Framework error

I'm trying to use pymongo and mongo's aggregation framework and here's what I got from Python:

OperationFailure: command SON([('aggregate', 'call_log'), ('pipeline', [{'$match': {u'user': 1}}, {'$project': {'date': 1, 'status': 1, 'number': 1, 'description': 1}}, {'$group': {u'first_number': {u'$first': u'$number'}, '_id': SON([(u'number', u'$number')]), u'avg_number': {u'$avg': u'$number'}}}])]) failed: exception: can't convert from BSON type 2 to double

Python's code cut:

    #Process grouping
    groups_list = []
    if "fields" in group_json:
        for  k, v in group_json["fields"].items():
            groups_list.append((k,'$' + k))
        if len(groups_list) > 0:
            obj_group_json.update({'_id': SON(groups_list)})
            if "aggregate" in group_json:
                for field in group_json["aggregate"]:
                    if field['func'] == "count":
                        obj_group_json.update({"count_" + field['name']: {'$sum': 1}})
                    else:
                        obj_group_json.update({field['func'] + "_" + field['name']:
                                                       {'$' + field['func']: '$' + field['name']}})

Do you have any ideas why?

Upvotes: 1

Views: 509

Answers (1)

Ross
Ross

Reputation: 18111

BSON type 2 is a string, so it seems to throw an error converting it to an integer.

Can you output: obj_group_json? Also, check your data - do you have any number data that isnt a number?

Upvotes: 1

Related Questions