0

I am trying to generate a report from our mongo db that tallies up the unique visits by country code per referral site. I'd like to use aggregation as I've heard it is quite fast and performance here is an issue.

We have an account db that has a country code and last referral site associated with each account.

{
   account:"user123",
   CountryCode:"CA",
   ReferalSite:"Google",
   lastLogin:"someisodate"
}

Conceptually, I can write the javascript in a few minutes.

For each unique vistor in accounts db;
    visits[vistor.country_code][vistor.refferal_site]+= 1;

Is this query possible with a db.accounts.aggregate()? Or is a map/reduce the better way to go about this.

Thanks in advance,

1
  • Looks like you have two collections, accounts and visits. Can you provide sample document from each? Neither map/reduce more agg framework can query more than one collection at a time though. Commented Aug 28, 2013 at 16:52

1 Answer 1

1

You can run two groups one after another :

    db.collection.aggregate([
        {$group:{_id:{account:'$account', CountryCode:'$CouintryCode', ReferalSite:'$ReferalSite'}}, {number:1}},
        {$group:{_id:{CountryCode:'$_id.CountryCode', ReferalSite:'$_id.ReferalSite'}}, {number:{$sum:'$number'}}}])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.