performance - Execute bulk find queries with sorting and limit mongo -
i have documents contain category field , want take top 10 documents each category based on popularity, seniority , rise in popularity. plan on using votes
field within document determine popularity, _id
field seniority , votesperday
field determine rising in popularity. there total of 12 categories.
typical documents this.
{ name : 'alpaca', category : 'blue', votes : 500, _id : object.id, votesperday : 50 } { name : 'muon', category : 'green', votes : 100, _id : object.id, votesperday : 20 }
i have object needs store categories , within each category store popular, newest , rising in popularity. object refreshed every 24 hours.
where running trouble whenever want query mongo 12 different categories.
i have tried have loop run 3 queries each category , store results once arrive fails since seems on loading mongo server , crashes.
the architecture of object trying build this.
var myobj = {category1 : { newest : [0,2....9], mostpopular : [0,2....9], risinginpopularity : [0,2....9] } ..... 12 of such objects};
the way thought of performing queries (although bit uncomfortable doing way) was.
categories.foreach(function(category){ var query = {category : category} var sort = //sorting criterion newest performquery(query,sorting,function(results){ myobject[category].newest = results }); sort = //sorting criterion popular performquery(query,sorting,function(results){ myobject[category].mostpopular= results }); sort = //sorting criterion rising in popularity performquery(query,sorting,function(results){ myobject[category].risinginpopularity = results }); //limit has set 10 documents });
so question how best perform type of mass query need obtain documents based on category , retrieve top 10 documents have votes, added (based on _id
), , have votesperday
.
Comments
Post a Comment