Aggregation with condition in embedded documents in mongo db

I am stuck in aggregation in mongodb. The premise is that I have to get data for certain ads within a time range.

So, suppose I request ads in the range from April 22 to April 24, this is what I should get, summing up expenses from source2 and income, session, rebounds, etc. from source1.

[{   "_id" : ObjectId("560bbd5dfabc614611000e95"),
    "spend": 470,
    "revenue": 440,
    "sessions": 3
},....

]

Here is the query I'm trying to make that gives me the correct data, but takes a very long time - 24 seconds for a total of 22k.

db.getCollection('tests').aggregate([{
  $match: {
    ad_account_id: 40
  }
}, {
  "$unwind": "$source1"
}, {
  "$unwind": "$source2"
}, {
  "$group": {
    "_id": "$internal_id",
    "transactionrevenue": {
      "$sum": {
        "$cond": [{
          "$and": [{
            "$gte": [
              "$source1.created_at", ISODate("2015-04-22T00:00:00.000Z")
            ]
          }, {
            "$lte": [
              "$source1.created_at", ISODate("2015-04-25T00:00:00.000Z")
            ]
          }]
        }, "$source1.transactionrevenue", 0]
      }
    },
    "sessions": {
      "$sum": {
        "$cond": [{
          "$and": [{
            "$gte": [
              "$source1.created_at", ISODate("2015-04-22T00:00:00.000Z")
            ]
          }, {
            "$lte": [
              "$source1.created_at", ISODate("2015-04-25T00:00:00.000Z")
            ]
          }]
        }, "$source1.sessions", 0]
      }
    },
    "spend": {
      "$sum": {
        "$cond": [{
          "$and": [{
            "$gte": [
              "$source2.created_at", ISODate("2015-04-22T00:00:00.000Z")
            ]
          }, {
            "$lte": [
              "$source2.created_at", ISODate("2015-04-25T00:00:00.000Z")
            ]
          }]
        }, "$source2.spend", 0]
      }
    }
  },
}]);

, , 1 ? 24 , 22 .... , , ( ), , 4 , - ?

, mongodb?

, , , . , .

{
    "_id" : ObjectId("560bbd5dfabc614611000e95"),
    "internal_id": 1,
    "created_at" : ISODate("2015-04-21T00:50:02.593Z"),
    "updated_at" : ISODate("2015-09-15T12:20:39.154Z"),
    "name" : "LookalikeUSApr21_06h19m",
    "ad_account_id" : 40,
    "targeting" : {
        "age_max" : 44,
        "age_min" : 35,
        "genders" : [ 
            1
        ],
        "page_types" : [ 
            "desktopfeed"
        ]
    },
    "auto_optimization" : false,
    "source1" : [ 
        {
            "id" : 119560952,
            "created_at" : ISODate("2015-04-23T12:35:09.467Z"),
            "updated_at" : ISODate("2015-05-19T05:20:58.374Z"),
            "transactionrevenue" : 320,
            "sessions" : 1,
            "bounces" : 1
        }, 
        {
            "id" : 119560955,
            "created_at" : ISODate("2015-05-01T12:35:09.467Z"),
            "updated_at" : ISODate("2015-05-19T05:20:58.374Z"),
            "transactionrevenue" : 230,
            "sessions" : 10,
            "bounces" : 1
        }, 
        {
            "id" : 119560954,
            "created_at" : ISODate("2015-04-23T10:35:09.467Z"),
            "updated_at" : ISODate("2015-05-19T05:20:58.374Z"),
            "transactionrevenue" : 120,
            "sessions" : 2,
            "bounces" : 1
        }, 
        {
            "id" : 119560953,
            "created_at" : ISODate("2015-04-25T12:35:09.467Z"),
            "updated_at" : ISODate("2015-05-19T05:20:58.374Z"),
            "transactionrevenue" : 100,
            "sessions" : 3,
            "bounces" : 2
        }
    ],
    "source2" : [ 
        {
            "id" : 219560952,
            "created_at" : ISODate("2015-04-22T12:35:09.467Z"),
            "updated_at" : ISODate("2015-05-19T05:20:58.374Z"),
            "spend" : 300
        }, 
        {
            "id" : 219560955,
            "created_at" : ISODate("2015-04-23T12:35:09.467Z"),
            "updated_at" : ISODate("2015-05-19T05:20:58.374Z"),
            "spend" : 170
        }, 
        {
            "id" : 219560954,
            "created_at" : ISODate("2015-04-25T10:35:09.467Z"),
            "updated_at" : ISODate("2015-05-19T05:20:58.374Z"),
            "spend" : 450
        }
    ]
}
+4
1

, , source1 source2 "created_at". , , , .

, , $unwind. .

, . $unwind , . , "". , :

db.getCollection('tests').aggregate([
    { "$match": {
        "ad_account_id": 40,
        "$or": [
            { 
                "source1": {
                    "$elemMatch": {
                        "created_at": { 
                            "$gte": new Date("2015-04-22"),
                            "$lte": new Date("2015-04-25")
                        }
                    }
                }
            },
            { 
                "source2": {
                    "$elemMatch": {
                        "created_at": { 
                            "$gte": new Date("2015-04-22"),
                            "$lte": new Date("2015-04-25")
                        }
                    }
                }
            }
        ]
    }},
    { "$project": {
        "_id": 0,
        "internal_id": 1,
        "source": {
            "$setDifference": [
                { "$map": {
                    "input": { "$setUnion": [ "$source1", "$source2" ] },
                    "as": "source",
                    "in": {
                        "$cond": [
                            { "$and": [
                                { "$gte": [ "$$source.created_at", new Date("2015-04-22") ] },
                                { "$lte": [ "$$source.created_at", new Date("2015-04-25") ] }
                            ]},
                            "$$source",
                            false
                        ]
                    }
                }},
                [false]
            ]
        }
    }},
    { "$unwind": "$source"},
    { "$group": {
        "_id": "$internal_id",
        "transactionrevenue": { "$sum": { "$ifNull": [ "$source.transactionrevenue", 0 ] } },
        "sessions": { "$sum": { "$ifNull": [ "$source.sessions", 0 ] } },
        "spend": { "$sum": { "$ifNull": [ "$source.spend", 0 ] } }
    }}
])

:

{ "_id" : 1, "transactionrevenue" : 440, "sessions" : 3, "spend" : 470 }

, , , , . "type", , .

, , , $match . $match , , , , , " ". , . .

, , , , , , , , .

, , , . , , . , , . .

+1

Source: https://habr.com/ru/post/1609668/


All Articles