2014년 12월 25일 목요일

mapreduce on more than one field in mongdb

"MongoDB MapReduce"

The sample structure is as follows:

                   {
                         "_id" :1,
                         "USER_ID" : "101",
                         "BROWSER" : "Chrome",
                         "LOCATION" : "A"
                   }

If I  use aggregation and mapreduce in mongodb (group by "USER_ID", "BROWSER", "LOCATION" to count the total number of documents), what I could achieve is that


                  {
                        "USER_ID" : "101",
                         "BROWSER" : "Chrome",
                         "LOCATION" : "A"
                   },
                         "count" : 10000
                  
                  {
                         "USER_ID" : "101",
                         "BROWSER" : "Mozilla",
                         "LOCATION" : "B"
                   },
                         "count" : 20000



Now if I want total count of {"USER_ID" : "101"}, I need to get 2 counts as above n sum.

----------------What I need is total USER_ID count, total BROWSER count, total LOCATION count separately in a single hit to database(USER_ID is that the main key) .----------------------



When you say you want total X count for field X, you mean that you want the number of documents for each unique value of field X? That requires one aggregation call for each field, for example for BROWSER:

db.collection.aggregate([
    { "$group" : { "_id" : "$BROWSER", "count" : { "$sum" : 1 } } }
])

You can't get the count for all three in one call to the database given your current document structure.



How many unique user_ids, locations and browsers do you have?
Technically you can do this in one aggregation if the size of results to your first aggregation doesn't exceed 16MB.


댓글 없음:

댓글 쓰기