mongodb - How to count incomming links? -


i have documents looking this:

{     "url" : "http://example.com",     "links" : [         "http://example.com/foo",         "http://example.com/bar"     ] }, {     "url" : "http://example.com/foo",     "links" : [         "http://example.com/bar"     ] }, {     "url" : "http://example.com/lost",     "links" : [         "http://example.com/bar"     ] }, {     "url" : "http://example.com/bar",     "links" : [] } 

how aggregate url , count number of incoming links:

{     "url": http://example.com,     "count" : 0 }, {     "url": http://example.com/lost,     "count" : 0 }, {     "url": http://example.com/foo,     "count" : 1 }, {     "url": http://example.com/bar,     "count" : 3 } 

do have idea how can it?

this 1 tricky want count incoming links. can achieve desired result using map-reduce:

the map stage emit "1" every link targetted examined url. in addition, ensure every source url in result set, emit "0" source link:

map = function() {     (var idx = 0; idx < this.links.length; idx++) {         emit(this.links[idx], 1)     }     emit(this.url, 0) // ensure url in output set } 

after that, reduce step matter of summing multiple values if any:

reduce = function(key, values) {     return values.reduce(function(a, b){return a+b;}); } 

given sample data set:

> db.test.mapreduce(map, reduce, {out:{inline:1}}) {     "results" : [         {             "_id" : "http://example.com",             "value" : 0         },         {             "_id" : "http://example.com/bar",             "value" : 3         },         {             "_id" : "http://example.com/foo",             "value" : 1         },         {             "_id" : "http://example.com/lost",             "value" : 0         }     ],     "timemillis" : 1,     "counts" : {         "input" : 4,         "emit" : 8,         "reduce" : 2,         "output" : 4     },     "ok" : 1 } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -