Embedded documents

Create issue
Issue #17 resolved
simone corti created an issue

Hi I'm trying to get back an arrays from an object structure with embedded documents.

    "_id": ObjectID("559d286f27819c41006bf3e5"),
    "cellId": 1,
    "data": [
            "country": "0",
            "sms_in": 1
            "sms_in": 10,
            "country": "39",
    "ts": ISODate("2013-12-08T00:10:00.000Z")

The idea was to sum together all the sms_in values from all the countries using the following pipeline

pipeline = [{"$match": {"cellId" : 1, "ts" : start} },{"$group" : {"_id" : "$cellId", "total" : {"$sum" : "$data.sms_in"}}}]

but the returned value for total always contains 0. I think the problem is due to the usage of the 'data' embedded document. Is Monary able to manage embedded documents or something is wrong in my code? Thank you.

Comments (5)

  1. David Beach repo owner

    Have you tried this using the aggregation pipeline with the PyMongo driver directly? I'm not expert in the aggregation pipeline, but I would think that you need an $unwind stage to handle the values in each data array. The documentation for $sum says that it ignores non-numeric values, and in this case, it looks like you're passing an entire list of values from each document.

  2. simone corti reporter

    You are right.

    pipeline = [{"$match": {"cellId" : 1, "ts" : start} },{"$unwind" : "$data"},{"$group" : {"_id" : "$cellId", "total" : {"$sum" : "$data.sms_in"}}}]

    Works correctly. Thank you.

  3. Log in to comment