aokozlov
aokozlov

Reputation: 731

Mongodb merge results from two collections

I have two separate mongodb collections search and objects that logs some user actions like using search page and viewing object page on my website. Both of coollections have user cookie stored the same way:

{ 
    "_id" : ObjectId("55ac35499db5acdb1c8b4567"), 
    ...
    "user" : {
        "id" : "12007", 
        "cookie" : "LffDjUQHogfrihI2z/FlzQ", 
    },
    "createddate" : ISODate("2015-09-20T02:39:53.780+0000")
}

Now i need to count unique users in both collections day by day. I know how to count them in one collection:

db.objects.aggregate([
    { "$group": {
        "_id":{
            "$subtract": [
                "$createddate",
                { "$mod": [
                    { "$subtract": [ "$createddate", ISODate("1970-01-01T00:00:00.000+00:00") ] },
                    1000 * 60 * 60 * 24
                ]}
            ]
        },
        'users' : {$addToSet : '$user.cookie'},
        }
    },
    {$project: {_id : 0, date : '$_id', users : {$size : "$users"}}},
])

So there i get count of unique cookies day by day. The probjem is that not all people that made search go to object page (but some of them go) so i need to get unique set of cookies by two collections day by day. I can get daily arrays of unique cookies from each collection and merge and count them on application side but i'm sure it can be done on db side. Please help.

Upvotes: 1

Views: 681

Answers (1)

aokozlov
aokozlov

Reputation: 731

The only way is to do separate mongodb queries and merge them on application level.

Upvotes: 1

Related Questions